You are on page 1of 3

Android ListView Tutorial

Table of Contents

1. Understanding Adapter

2. Building ListView using ArrayAdapter

3. Building ListView using Custom Adapter


3.1. Create your custom row layout

3.2. Writing a custom adapter

3.3. Putting it all together

3.4. Output

4. Customizing ListView
4.1. Change ListView selection colour in Android

5. How to change the divider color in the ListView?


5.1. Changing ListView divider color and height

5.2. Using a drawable for ListView divider

5.3. Changing ListView divider color pragmatically

6. References

This post will walk you through Android ListView Tutorial for building simple and
customized ListView using different Android adapters.
List is one of the most common UI patterns, which is being used extensively to display the collection of
data elements in rows. In android ListView is a view group that displays a list of scrollable items. The
list items are automatically inserted to the list using an Adapter that pulls content from a source such as
an array.

1. Understanding Adapter
Adapter is basically bridge between UI components and the data source that fill data into UI
Component. Adapter is used to supply the data to like spinner, list view, grid view etc. For example, we
can create a list view from xml layout without data, and using adapter we can supply data sets to list

If you look at the above images you will certainly get an idea of list view. This kind of customizable list
views can be done using an adapter.

2. Building ListView using ArrayAdapter

This is the simplest way we can create a simple default styled list view from array of elements. To do
this there are three things to concentrate,

Find out what data you want to display in list: For our example, I am considered taking a
static array of strings. But for complex scenarios it can be a response from server, or data fetched
from database.


Declare the list view object in your xml layout: ListView is the user interface element we are
using to represent data to user. So in my example, the layout contains a list view. Make sure you
provide an appropriate id.


Now finally, feed the list view with the data sets: For this we use adapters. We can always
customize our adapter, but for to start lets make it simple. I am using Array adapter class
available in android.

Here is how my layout file looks like

<LinearLayout xmlns:android=""
tools:context=".ListActivity" >
android:layout_height="wrap_content" >

package com.javatechig.droid.ui;


public class ListActivity extends Activity {

String[] monthsArray = { "JAN", "FEB", "MAR", "APR", "MAY", "JUNE", "JULY",
"AUG", "SEPT", "OCT", "NOV", "DEC" };
private ListView monthsListView;
private ArrayAdapter arrayAdapter;
public void onCreate(Bundle savedInstanceState) {
monthsListView = (ListView) findViewById(;

arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, monthsAr


Output of the above code is