North Border Software

Home Tutorials Hints & Tips About

Android Apps and Training

Share on Facebook Share on Twitter

Getting Started in Android Development

Part 13 - Creating a ListView and making Toast

A ListView is a fundamental and very popular navigation tool within Android. A ListView simply provides a scrollable list of items and each item is clickable for navigation or to execute tasks. In this tutorial we will look at setting up a basic ListView. This ListView will be augmented in later tutorials and so in this tutorial list will simply be used to show a “Toast”. We will look at the “Toast” element later on in this tutorial.

We will use the activity_listview.xml layout and ListviewActivity class created in the last tutorial.

A ListView view can simply be dragged across to the layout from the design view or the XML text added as below. The finalized activity_listview.xml layout should look similar to the one below.

It simply contains a single ListView as a child of the layout. That is all there is to it. The real work with a ListView starts with the Java class. The first task is to associate the ListView in the layout to the java class. The code below, added to the ListviewActivity class will achieve this:

ListView listView = (ListView) findViewById(;

We next need to create a string array containing the text for each item in the list. The code below is added to the java class:

String [] values = new String[] {"item1", “item2”, “item3”….};

Initially, we will only add one item, “ScrollView”, to the list and the array’s variable name is “values”.

The next step is to create an array adapter. The code for our example is below:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,, values);

Working through the above, the variable name we have given to the array adapter is “adapter”. Several parameters are needed.

The first is the context. Since the ListviewActivity is the context we wish to use, then we can use the “this” keyword. The next parameter is the reference to the layout that we wish to use to define the appearance of the ListView. We are using one of the predefined layouts in Android. This is called “android.R.layout.simple_list_item_1”. There are other predefined Android ListView layouts. We can also define our own. The next parameter, “” defines the view that will be used to display the text. Finally, the string array holding the ListView item text is passed to the adapter.

The array adapter that you have just set up then needs to be associated with the listview variable using the line of code below:


The final Java task is to set an on item clicked listener for the “listview” variable using the setOnItemClickListener method. After this tutorial your ListviewActivity java class should look similar to the one below.

Within the setOnItemClickListener method, we have included an onItemClick override method. Essentially, this detects the position of the clicked item starting at position 0. A switch and case can be used to handle clicks from specific positions. At this stage we have chosen to display a “Toast”.

A “Toast” is a way for displaying a short, temporary pop up message. The format is below:

Toast.makeText(getApplicationContext(), "<msg>", Toast.LENGTH_SHORT).show();

Download Download tutorial set 1

© 2015 North Border Software All rights reserved
Privacy and Cookies Terms of Use Disclaimer Copyright
North Border Software Logo