Android parcelable tutorial: List and inner class

May 21, 2015
Topics covered

Android Parcelable

Android Parcelable List

Android Parcelable inner class

When we want to pass objects between two components in Android, these objects have to implements Parcelable interface. Android Parcelable is a serialization and deserialisation mechanism as we are used in Java. This post describes how we can use Parcelable interface to pass objects between two Android Activity.
We talked about some basic concepts of Parcelable in Android, in this post we will analyse some more complex example and we will look how we can parcel a List of objects or parcel a class that contains inner class.

Android Parcel object

We know that if we want that Android OS can "serialize" an object it must implement Parcelable interface. To make an object parcelable we have to implements two methods defined in Android Parcelable interface:
Moreover, we have to provide Parcelable.Creator that is used to create an instance of the class from the Parcel data.
So let us suppose we have a simple class that contains a contact info:

public class ContactInfo {

    private String name;
    private String surname;
    private int idx;

    // get and set methods
}

So to make this object parcelable we have:

public class ContactInfo implements Parcelable {

    private String name;
    private String surname;
    private int idx;

    // get and set method

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(surname);
        dest.writeInt(idx);
    }

    // Creator
    public static final Parcelable.Creator CREATOR
            = new Parcelable.Creator() {
        public ContactInfo createFromParcel(Parcel in) {
            return new ContactInfo(in);
        }

        public ContactInfo[] newArray(int size) {
            return new ContactInfo[size];
        }
    };

    // "De-parcel object
    public ContactInfo(Parcel in) {
        name = in.readString();
        surname = in.readString();
        idx = in.readInt();
    }
}

Android Google map: Add weather data tile

March 30, 2015
Topics covered

NFC

Google map in Android

Weather tile

Add tile to a map

This post describes how to add weather layer to google map. We want to add weather data to a map or in other words add to Android google map a tile so that the app; shows clouds, temperature, pressure and so on.
In the previous post we talked about how to get weather information and how to parse the data to create a UI, now we want to add other weather tile to a map. To do it, we start creating a sample project that contains a map activity.
If we use Android Studio (as i suggest) it is very simple and fast.

Create Android Map project in Android Studio

The first step is creating a google map project in Android. As always, we select File->New project and after inserting some project information we land to this page:


Now select Google Map Activity. Android Studio will create all the things to handle google map in our project. The last thing to do is to add the key to use the map. If you look in the files inside the project you will find the way to create your debug key. Once you have the key, it is time to use the map.

NFC Android: Read NDEF Tag - Text,Link, Smart Poster

March 16, 2015
Topics covered

NFC

NDEF

Read NFC content

NFC payload

Smart poster, text, link

This post describes how to read NFC tag. Android SDK provides a set of API that can be used to read the NFC payload, anyway looking at the NFC specification, we can notice there are several type of NFC payload depending on the type of the tag. Android SDK doesn't provide any support to parse the payload and extract information, so in this post we will describe how we can read the NFC tag content. As we said in the previous post, explaining how to use NFC in Android, there are several type of NFC (NDEF) tag:
  • NFC Forum well-known type [NFC RTD] 
  • Media-type as defined in RFC 2046 
  • Absolute URI as defined in RFC 3986 
  • NFC Forum external type [NFC RTD]
It is possible to know the NFC type using the last three bytes in the NFC header or simpler using:

short tnf = record.getTnf();

Comparing the tnf (Type Name Format) with all possible combination, we can know the record type. In the code above record is an instance of NdefRecord.

NDEF Record Structure

Before analysing how to read NDEF content, it is important to know the NDEF record structure. The picture below shows the structure:


The most important byte (7th) is the Message Begin byte, this byte is 1 if the it is the starting message otherwise is zero. The 6th byte is the Message End, this byte is 1 if the this record is the end record otherwise is 0. SR is the Short Record and it is 1 if it is a short record. This information are important if we want to handle the NDEF tag data correctly.
We know that Android SDK provides the method getPayload() that returns an array of bytes that represent the tag content. We have to read and parse this array to extract information.
Let's start from the simplest record structure: text record.

First steps with NFC in Android - NFC Android App tutorial

March 8, 2015
Topics covered

NFC

NDEF

NFC Foreground dispatch

NFC payload

NFC filter

This post describes how to create NFC Android app. The NFC technology stands for Near Field Communication and you can find the specification at NFC Forum. In this first post, we will analyse some basic aspects of NFC in Android and we will describe how we can implement an NFC app that handle tags.
If you want to experiment NFC, there are several web site where you can buy NFC with a few euro.
NFC can be used in different situation: we can use it to turn on our Wifi when we are at home or launch task actions and so on.


We will focus our attention on NDEF data that is a special type of NFC tag. There are some basic steps we have to follow before using the NFC.


NFC Filter

When we use NFC tag,, the first thing we want is our app is notified when we get near a NFC tag. To this purpose we use a intent filter. Android SDK provides three different filter that we can use with different level of priority:

  • ACTION_NDEF_DISCOVERED
  • ACTION_TECH_DISCOVERED
  • ACTION_TAG_DISCOVERED
We focus our attention on ACTION_NDEF_DISCOVERED, that has the highest level of priority. As said, our goal is being notified when the smart phone is near a NFC tag and, if we have only this app installed and capable to handle this NFC tag, we want that the app starts immediately. To do it, we register the filter in the Manifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.survivingwithandroid.nfc" >

    ....
     <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data  android:mimeType="text/plain"/>
     </intent-filter>
<manifest>

Android Text to Speech (TTS)

February 16, 2015
Topics covered

Text to Speech (TTS)

Pitch

Speed rate

Talking app

This post describes how to use text to speech (TTS) in Android. It isn't so common to find a post explaining how to use it and it is a part of Android not much covered. In my opinion, TTS is very interesting because it can add some nice features to an app. Text to Speech is a feature of Android platform that can be used to "read" the words and make the app talking, or more in detail to synthesize text.
In this post, i want to cover how to implement TTS in Android and how we can control some interesting aspects of speech engine. We want to code an app that has a EditText widget so that we write the words that have to be read and some controls to modify the speech engine.



Text to speech Engine

The first thing we have to do to use the TTS in our app is initialise the engine. The class that controls the engine is called TextToSpeech,
engine = new TextToSpeech(this, this);

where the first parameter is the Context and the other one is the listener. The listener is used to inform our app that the engine is ready to be used. In order to be notified we have to implement TextToSpeech.OnInitListener, so we have:

public class MainActivity extends Activity implements TextToSpeech.OnInitListener {
    ....
    @Override
    public void onInit(int status) {
        Log.d(&Speech&, &OnInit - Status [&+status+&]&);

        if (status == TextToSpeech.SUCCESS) {
            Log.d(&Speech&, &Success!&);
            engine.setLanguage(Locale.UK);
        }
    }
}
We use the onInit as callback method, and when the engine is ready, we set the default language that the engine used to read our sentence.