Surviving w/ Android

Android development blog
Tutorials about Android dev topics

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> {
    ....
}

Android chart tutorial: AChartEngine

Topics covered

Android chart

AchartEngine

Linear chart

Bar chart

Range Bar chart

WeatherLib

In this post, I will describe how to use AchartEngine. This is a great library for Android that help you to create charts. It supports several chart types, just to name a few:
  • line chart
  • area chart
  • bar chart
  • pie chart
  • combined chart
and so on.
This library helps you in every aspects when you create a charts so you don’t need anything else to create interesting charts.

Getting started

If you use Android Studio you can download directly the jar containing all the classes and add it to your project. When the download is completed, you should add the library under libs folder so that I can be included automatically into your project. Now you are ready to use the lib!
When you create a charts you need usually a set of data that has to be drawn in the chart, in this case to have real values and to not re-invent the wheel we can suppose we get these values using WeatherLib so that we will plot the atmospheric parameters (like temperature, and pressure..).
There some basic concepts that stand behind this library and they are important so that you can use it:
  • Dataset (The set of data you have to draw in your chart)
  • The view (or the type of chart you want)
  • Renderer (It controls how the view is drawn, settings some parameters you can change the way the charts looks like. There are two types of renderer: one that controls the rendering of the dataset and another one that controls how the main chart aspects look like (i.e. axis, labels and so on)
  • Chart factory (combines the dataset and the renderers to create the charts. The chart can be created inside an Activity or the factory can return a View.)

Create a jar libray with gradle using AAR info

Topics covered

Android studio - Gradle

How to create jar

build.properties

Eclipse jar

Some posts ago, I talked about how to use gradle to push an aar to maven central. If you remember, we had to modify some files and so on, but the work we have to do helps other developers to simplify their development when they want to use our code/library. When our code is pushed to the maven central as aar we can resuse it as libray simply setting a gradle dependency.
For example, if we want to use Weatherlib we have to write:
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.+'
    compile 'com.survivingwithandroid:weatherlib:1.3.1'
}
very simple!
Anyway this is true if we use Android Studio, but what if we use Eclipse or something else. In some cases it is easier to have a classic jar that can be imported in our Eclipse project and add a dependency in our project to this jar.
If we use Android Studio this jar isn’t created easily (AFAIK) and I had to slightly modify the build.gradle to create the jar. I looked around on the net and I found a solution that I re-adapted so that we can reuse the information stored in the properties file. If you remember from the post about aar and gradle (if not look here), there are two properties files that I show it for simplicity:
POM_NAME=Android Weather Library
POM_ARTIFACT_ID=weatherlib
POM_PACKAGING=aar

and
VERSION_NAME=1.3.1
VERSION_CODE=6
GROUP=com.survivingwithandroid

POM_DESCRIPTION=Android Weather Lib
POM_URL=https://github.com/survivingwithandroid/WeatherLib
POM_SCM_URL=https://github.com/survivingwithandroid/WeatherLib
POM_SCM_CONNECTION=scm:git@github.com:survivingwithandroid/weatherlib.git
POM_SCM_DEV_CONNECTION=scm:git@github.com:survivingwithandroid/weatherlib.git
POM_LICENCE_NAME=The Apache Software License, Version 2.0
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENCE_DIST=repo
POM_DEVELOPER_ID=survivingwithandroid
POM_DEVELOPER_NAME=Francesco Azzola

So I would like to use this information to create a jar with the name equals to the POM_ARTIFACT_ID combined with the VERSION_NAME, and this jar should be created under a specific directory. So we have to add under android section in build.gradle:
android {
    ...

    sourceSets {
        main {
            java {
                srcDir 'src/main/java'
            }
            resources {
                srcDir 'src/../lib'
            }
        }
    }
    ..
}



and after the dependencies section:
task clearJar(type: Delete) {
    delete 'build/libs/' + POM_ARTIFACT_ID + '_' + VERSION_NAME + '.jar'
}

task makeJar(type: Copy) {
    from('build/bundles/release/')
    into('release/')
    include('classes.jar')
    rename ('classes.jar', POM_ARTIFACT_ID + '_' +  VERSION_NAME + '.jar')
}

makeJar.dependsOn(clearJar, build)

Now if you run the task makeJar, AS will create a jar under the directory called release.

If you want to have the build.gradle file you can get it here