Surviving w/ Android

Android development blog
Tutorials about Android dev topics

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

How to develop an Android Weather app using Weatherlib

Topics covered

Weatherlib

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.

Android SwipeRefreshLayout Tutorial

Topics covered

Android SwipeRefreshLayout

Refresh ListView

Reresh UI

In this post, we will describe SwipeRefreshLayout component. This component should be used whenever the user can refresh the UI using swipe gesture. In a previous post, we talked about another method to refresh the UI that we called shake to refresh, where the user shakes his smartphone and using accelerometer sensor the app refresh the UI. We talked about a custom implementation of this refresh pattern in this post where we implemented some like it.
The SwipeRefreshLayout component is a standard implementation provided by the SDK and it is already used in some App provided by Android (i.e Gmail).

Introduction

This component accepts only one child: the one we want to refresh. It uses a listener mechanism to inform the listener that holds this component that a refresh event occurred, so in other word our Activity, for example, has to implement an interface to be notified. The Activity is responsible to handle the refresh event and refreshing the corresponding View. If the listener, once it receives the event, determines that the refresh process should take place and wants to show a “refresh animation”, it has to call setRefrshing(true), otherwise it can cancel the animation calling setRefreshing(false).

How to use SwipeRefreshLayout

Now we know how this component works, we will create a simple example to show how to use it. Let us suppose we want to generate a random number as the user uses a vertical swipe gesture:
android_swipetorefreshlayout