Surviving w/ Android

Android development blog
Tutorials about Android dev topics

Control Sphero using Temperature Sensor in Android

Topics covered

Sphero 2

Sphero Android SDK

Android temperature sensor

Android Service

Android Broadcastreceiver

Android AlarmManager

One of the most interesting topic around Android it is how we can connect our smart phones to other devices or smart devices to get information from them or to control them.
In this post I want to introduce a new way to use our Android phone and explain how we can use it to control Sphero ball.

Introduction

In this Android project, I want to describe how we can integrate the temperature sensor inside the smart phone to control the Sphero ball color. In other words, I want to change the color ball according to the temperature measured by the smart phone even if the smart phones is in the stand by mode or the Activity is not in the foreground.
This is an interesting project because it can be used to describe some important concept:
and finally, but not less important, how to connect and use Sphero ball with its SDK. What we want to design an app like shown below:



Android promoted Actions: Floating action button (FAB)

Topics covered

Promoted Actions

Floating Action Button (FAB)

Custom component

Material design

Recently new design pattern were introduced in Android. One of the most known pattern is the Promoted Actions. They are actions that  are directly visible in the UI instead of using action bar button; for this reason these actions are called promoted, they can be easily accessed and define the main action in the current UI. For example, if we are using an email app and we are listing the inbox folder, one promoted action can be a new mail. The visual pattern is called Float Action Button (or FAB) because the promoted action can be represented using a floating circular button on the UI.
This type of design patterns are available in Android L but we can create a floating button also in previous Android version. Let us suppose we have a list of elements in our UI, the promoted action can be ‘Add new item’, so we can have something like the pic shown below:

android_floating_action_button_1

There are several way we can use to crate a floating action button.

Floating action button using ImageButton

The first way to create a FAB is using the ImageButton, we simply add the ImageButton to our UI layout and exploit some new features provided by Android L.
<ImageButton
android:layout_width="56dp"
android:layout_height="56dp"
android:src="@drawable/plus"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:tint="@android:color/white"
android:id="@+id/fab"
android:elevation="1dp"
android:background="@drawable/ripple"
android:stateListAnimator="@anim/fab_anim"
/>

If we look above, we can notice it is a normal ImageButton that we place usually at lower right corner. There are some aspects we should consider:
  • Background
  • Shadow
  • Animation

HAXM Module problem in OS X Yosemite: Fix

Topics covered

HAXM

Fix Yosemite HAXM

Fast android emulator

OS X AVD

After upgrading my Macbook Pro to Yosemite beta version, i experienced some problems starting Android emulator because HAXM didn’t work anymore. It was really a problem, because the emulator was really slow and hanged for long time. The first step i made was trying to re-install the HAXM. Even if the installation completed correctly and everything was compatible with HAXM requirement, at the end of the installation process i get i message explaining that the HAX installation couldn’t work properly. I decided to find a solution on the net and finally i find out something that could help me. In this post i’d like to explain how i made HAXM working on Yosemite. Some fo these information are extracted from here.

HAXM Problem

The first step is to check why HAX module doesn’t work. Instead of starting the emulator from Android Studio, you can start it from the command line and set some parameters.

./emulator -avd your_avd -verbose

Using this command you start the emulator and it shows the logs on the stdout. If you take some time reading them, you can notice that after some lines describing the type of AVD you are starting and so on there is some lines like those shown below:


As you can see the log shows a message saying that is impossible to open HAX module even if it is installed correctly.

We can investigate more the problem, using the command kextload:

sudo kextload -bunlde-id com.intel.kext.intelhaxm

the result is shown below:



It doesn't help us much, it just says it is impossible to load the module. The next step is using kextutil:

sudo kextutil -bunlde-id com.intel.kext.intelhaxm

It returns the information shown below:


 Finally we found out the reason, it is a signature problem. I guess it is because this module isn't certified for OS Yosemite.

Android Listview with multiple row layout

Topics covered

Android ListView

Multiple layout

getViewTypeCount

getItemViewType

Android BaseAdapter

In many posts we talked extensively about ListView and how to handle it: creating custom adapter or handling user interactions.
In this post, I will cover another aspect related to the ListView that was not covered in the past: ListView with multiple row layout. By now we have seen rows having the same layout, anyway it is possible to have rows with different layouts.
We want to have something like the pic shown below:

android_listview_multiple_layout
In this app, we show a Contact list that has two different layouts one that uses an image and another one without image. Even if the example is quite simple, it is enough to understand how to use several ListView methods to get this result.

Android RecyclerView

Topics covered

Android RecyclerView

RecyclerView.Adapter

View holder pattern

ListView differences

RecyclerView is one the two UI widgets introduced by the support library in Android L. In this post I will describe how we can use it and what’s the difference between this widget and the “classic” ListView.
This new widget is more flexible that the ListView but introduces some complexities. As we are used RecyclerView introduces a new Adapter that must be used to represent the underlying data in the widget. This new adapter is called RecyclerView.Adapter. To use this widget you have to add latest v7 support library.

Introduction

We know already that in the ListView to increase the performance we have to use the ViewHolder pattern. This is simply a java class that holds the references to the widget in the row layout of the ListView (for example TextView, ImageView and so on). Using this pattern we avoid to call several times findById method to get the UI widget reference making the ListView scrolling smoother. Even if this pattern was suggested as best-practice we could implement our Adapter without using this pattern.
RecyclerView enforces this pattern making it the core of this UI widget and we have to use it in our Adapter.

The Adapter: RecyclerView.Adapter

If we want to show the information in a ListView or in the new RecyclerView we have to use an Adapter. This component stands behind the UI widget and determines how the rows, in the ListView, have to be rendered and what information to show. Also in the RecyclerView we have to use an Adapter:
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyHolder> {
    ....
}