Android Listview Example

Today, we are going to see about a simple listview example. In Android, Listview is used to show a list of items in a vertically scrolling list.  Learn a listview of android array in this tutorial.

For instance, in a Registration form when we are selecting professions a list of items will be displayed. We can use Listview to display the list of items.

Your XML file should look like

[sourcecode language="xml"]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
<ListView android:id="@+id/ListView01"
android:layout_height="wrap_content" />


Your Java code looks like

[sourcecode language="java"]

public class ListviewExample extends Activity
private ListView lv1;
private String lv_arr[]={"Android","iPhone","BlackBerry","AndroidPeople"};
public void onCreate(Bundle icicle)
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));


You can also customize your listview. Click here to see custom listview example

The output will look like



Hi fernie, look at the below

Hi fernie,

look at the below link

Here you can see about how to add columns in listview. Here they have two columns, one is image & another is text.

First of all thanks for your

First of all thanks for your comments!..
Ya, you are right. We can use like that too..

Hi noelle Yes u can add

Hi noelle
Yes u can add OnClickListner or we can also use setOnItemClickListener to trigger a action on a particular item's click!
Yourlistview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
// your actions

Alex, The simple_list_item_1

The simple_list_item_1 is used to design the list items.
If you use simple_list_item_checked , there will be a option to check the list items.
Same like that we can use simple_list_item_multiple_choice ,simple_list_item_single_choice,etc..
Questions ?

Hi noelle, post your doubt as

Hi noelle,
post your doubt as comments,so that it will be useful to others too.Feel free to post doubts.

Pranay, Look at the 12th

Look at the 12th line
lv1.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1 , lv_arr));
This line helps us to set listview elements. By using this one more time we can able to change the listview elements.

For example :-
Use arraylist in ListView instead of String Array. Create a Button and set onClickListener to that Button.
In that onClickListener add elements to the arraylist and set it to ListAdapter.

Questions ?..

Parena, By using


By using getCheckedItemPositions() you can get the selected items from listview with multiple choice.
Here is a simple example of listview multiple choice

You can see more listview example

Hi Sindhu,

Hi Sindhu,
prbly this example should help u

Cheers :)

Hi sindhu, To

Hi sindhu,
To remove the latest intent just call finish() method. But use some thing like global variable to get the data from listview clickevent and store the data to global variable so that u can access even in the home page.

Hi madhuri ,

Hi madhuri ,
Try to have global string array and keep collecting the item as when you click on the check boxes.later u can use it any activity.

Hi mash, paste ur log cat

Hi mash,
paste ur log cat here so that we can find what causing the application to crash.

Hi Askile, If

Hi Askile,

If you use textview in listview and then by default it's multiline. The string will adjust itself. For onclick see this example

You can store large strings

You can store large strings in DB. its not an issue. if it runs out of space split into 2 paragraph and have them in two column.

Gowri, Instead of using

Instead of using String Array use ArrayList. By using arraylist we need not to mention size of arraylist.
After entering text in edittext click the add button.
In add button onClick() write
lv1.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1 , arrayList));

After adding the value to arrayList. Just set one more time the listview adapter.

Gowri, wat error you are

wat error you are facing ?...
can you please paste the logcat error here..?



@satya, Refer the below link


Refer the below link

It may help you..

@satya, At very first time


At very first time give only


public void onCreate(Bundle icicle)






in onclick listener

add the values to string array and then give

lv1.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1 , lv_arr));

@Gayathri, Refer this link


Refer this link

This example will explain how to add values to listview in run time using Edittext, Button & ListView.

@satya, Refer this link for


Refer this link for listview onclick

@satya, Look at this example


Look at this example

Sliding Drawer for Android.

@satya, Give fixed size for


Give fixed size for listview to get 2 listviews at a time.