Surviving w/ Android

Android developer blog
Android development tutorial that helps you to become an android developer

Android Shake to Refresh tutorial

by Francesco Azzola, April 15, 2014
Topics covered

Shake to refresh

Android sensor

Accelerometer

SensorManager, SensorEventListener

In this post we want to explore another way to refresh our app UI called Shake to Refresh. We all know the pull-to-refresh pattern that is implemented in several app. In this pattern we pull down our finger along the screen and the UI is refreshed:
android_pull_to_refresh
Even this pattern is very useful, we can use another pattern to refresh our UI, based on smartphone sensors, we can call it Shake to refresh. Instead of pulling down our finger, we shake our smartphone to refresh the UI:
adroid_shake_to_refresh

Android WeatherLib: A new lib to create Android Weather App easily and fast

by Francesco Azzola, April 6, 2014
I've published at github a new library that makes easier to develop weather app providing a framework to get actual and forecast weather.
The lib helps you to focus only on the app without worrying about details regarding:


  • Remote service invocation 
  • HTTP request response handling 
  • Thread and ANR problems 
  • Search city by current geographics coordinates
  • Search city by name pattern
  • Weather provider protocol parser
  • Retrieve weather icons
This is a library project that helps developers to create easily weather app without worrying about querying and extracting information from remote weather servers. It is very simple to use and you can easily extend it to support different weather provider or you can customize it according to your needs.
By now it supports two main weather provider:


I've implemented a demo app (under demo module) that shows how to use the lib. At github there is moreover a short introduction about the main concepts that stand at lib base and how you can use it.


If you like it please help me to share this information. 
I've created a community where you can help me to improve the lib, discuss about it or give  some tips or ask for  new features. If you are interested please join it.

Project repository

G+ Community

Android app development :weather app with forecast

Using Android Location API in Weather App - Search city

by Francesco Azzola, April 1, 2014
Topics covered

Android Location API

Search city in openweathermap

LocationManager

Geographic coordinates

In this post I would like to describe how to search a city using openweathermap to get weather conditions. There are two way we can use to find a city:
  • Using name pattern
  • Using geographic coordinates (Android Location API)
We will introduce, moreover, some concepts about Android location API and Location-based service.

Searching city by name

In this kind of search, an user inserts a part of city name and the app will show all the possible result matching the pattern. From the UI perspective, we want to obtain something like the image shown below:
android_weather_app_search_city
As first step, we can use EditText so that the user can insert the city name:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

....
    <EditText
        android:id="@+id/cityEdtText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="5dp"
        android:imeOptions="actionSearch"
        android:inputType="text" >
        <requestFocus />
    </EditText>
....

    <ListView android:id="@+id/cityList"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" 
              android:layout_below="@id/txt1"
              android:layout_marginTop="3dp"/>

</RelativeLayout>

Android Remote Service Tutorial:AIDL–how to structure the Project and lib

by Francesco Azzola, March 27, 2014
Topics covered

Android Remote Service

AIDL

Inter process comunication (IPC)

Android Project Structure

In this post, I will describe how to use Remote Service in Android. This kind of services are services that can be consumed by other processes using RPC (Remote Procedure Call). In the other post we talk about Local Service, in other word the application that hosts the service can consume it. AIDL Services are useful when we want to create some new functionalities and we want to distribute them as library. An interesting aspect, we should consider when developing an AIDL service is that it can be called/consumed by components running in different processes. To support IPC (Inter Process Communication), in Android we have to define an interface that describes the methods that will be exposed to the client. To create this interface we use AIDL (Android Interface Definition Language).
Considering that these remote services could be distributed as library, we have to choose what we want to give to the client (as jar) so that it can call and consume our service. It is important, then, have the right project structures so that we can create the client jar that holds only the necessary classes. In the rest of this post we will focus our attention on this aspect too.
As example, we will use the same example described last time where we get stock quotes.

Define a Remote AIDL Service

In order to create an AIDL Service we have:
  1. define and create the service interface using AIDL
  2. Implement our service and override onBind method returning our interface
  3. Define objects that the client and the server exchange and deconstruct them at low OS level so that they can be marshaled and un-marshaled. In other words, our classes have to implement Parcelable interface.
  4. Configure our service in Manifest.xml file
In our example, we know we just want to know the stock quote, so for simplicity our interface is just made by only one method called getQuote. In this method we pass the Stock pojo class that holds the information about the stock code and the values that will be filled by our service in response. Our pojo class is called Stock. So considering all things said, we have that the AIDL is:
package com.survivingwithandroid.aidlservicetutorial.service;

import com.survivingwithandroid.aidlservicetutorial.service.Stock;

interface IStockService{
    void getQuote(Stock stock);
}

notice that at line 3 we simply import the Stock definition and at line 6 we define our method. On the other hand, we have to define in AIDL our Stock pojo:
package com.survivingwithandroid.aidlservicetutorial.service;

parcelable Stock;

In this way, we defined our service interface. If you use Eclipse you can put these two files under source and the package name. Eclipse will create everything you need to use the service.