Surviving w/ Android

Android development blog
Tutorials about Android dev topics

Android Listview with multiple row layout

Topics covered

Android ListView

Multiple layout



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:

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


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.


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


Linear chart

Bar chart

Range Bar chart


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

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 ''
    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_DESCRIPTION=Android Weather Lib
POM_LICENCE_NAME=The Apache Software License, Version 2.0

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) {
    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

How to develop an Android Weather app using Weatherlib

Topics covered


Tutorial about weather app

Opensource lib

Openweathermap, Yahoo! Weather, Weather Underground

Some users asked to me some details on how to use the Weatherlib. In this post want to describe how to use it. This is an Android lib that helps to develop fast and  easily weather app. This post describes the main concepts that stand behind the lib so that you can understand it better. If you are not interested about the internal library structure you can jump to the part that describes how to code a weather client:
 'Setup the dev enviroment'.

Weatherlib structure

The library is built by three different layers:

Data model 
Weather providers
Weather client

The first layer, the data model, represents the information extracted by different weather provider. It abstracts this information so that, independently the weather provider you choose, you have always the same information presented in the same manner and with the same relations between them.
Weather providers are connectors toward the weather provider. By now the library supports three weather provider:
  1. Openweathermap
  2. Yahoo! Weather
  3. Weatherunderground
This layer implements the logic necessary to extract information from the weather provider, parsing the data retrieved and at the end of the process these providers populate the data model.
The last layer is the client layer. This layer implements the logic to connect the weather provider (i.e. using HTTP connection) and handle the connection errors and so on. Once the data is retrieved the client uses the weather provider to parse the data.
Each layer can be extended and customized easily so that you can support other weather providers or implements a different way to connect to the providers. We will see later how to do it.