Android ListView Tutorial

Android ListView is Viewgroup of that create list of scrollable item.A view that show item in a vertically  scrolling list.

Now create project with Android Simple Listview.
create row.xml file in res->layout folder of your project.

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android=""
    android:textSize="15dp" >

now open your file in your project src folder. and 

public class MainActivity extends ListActivity {

see here when we extending ListActivity to MainActivity then thet layout file must have ListView with id 


don't forget this because it cause exception your content must have listview whose attributes or id is

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
    android:orientation="vertical" >

        android:layout_alignParentTop="true" >


Now using ArrayAdapter you can bind data with listview.ArrayAdapter is class that works to bind data to listview.You pass data in Constructor of this class.

private String[] INDIAN_STATE = { "Gujarat", "Andra Pradesh",
"Arunachal Pradesh", "Assam", "Bihar", "Chhattisgarh", "Goa","Haryana", "Himachal Pradesh", "Jammu and Kashmir", "Jharkhand", "Karnataka", "Kerala", "Madya Pradesh", "Maharashtra", "Manipur","Meghalaya", "Mizoram", "Nagaland", "Orissa", "Punjab",
"Rajasthan", "Sikkim", "Tamil Nadu", "Tripura", "Uttaranchal", "Uttar Pradesh", "West Bengal" };

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.row, INDIAN_STATE);

now you just call setListAdapter to set Adapter to listview.


its display the indian state in listview and you can change your color of state name and background using row.xml file.they have textview,change its color or background.

Now,when listview is displayed its more important how to check which item clicked in listview and get selected or clicked value.there are OnItemClickListener for ListView.

but when you extends ListActivity then just ovveride onListItemClick  method.and there first parameter is listview.second is view which is row or item view.third is position of item.and last is id of row or item.Using 

String str =l.getItemAtPosition(position).toString();

we can retrieve value of item.


protected void onListItemClick(ListView l, View v, int position, long id)                               {
   super.onListItemClick(l, v, position, id);
   String str =l.getItemAtPosition(position).toString();
  Toast.makeText(MainActivity.this, str,Toast.LENGTH_SHORT).show();

now we discuss how to display listview without extending ListActivity.when we extends Activity there is no need to(not compulsory) your ListView have id android:id="@android:id/list".you can use your own list name
 you can get Listview using,

 ListView listView = (ListView)findViewById(;

and set Adapter and OnItemClickListener to listView.
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> l, View v, int position,long id) {
   String str = l.getItemAtPosition(position).toString();
   Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();

DownLoad Full Source Code From Here
        DownLoad Source Code


No comments:

Post a Comment

Android Testing App

Alpha Testing is conducted within organization and tested by developer/ team of developers/ testers. This testing is close for public.  ...