Android ListView example
In Android, ListView let you arranges components in a vertical scrollable list.
In this tutorial, we will show you 2 ListView examples :
- Normal way to display components in
ListView. - Custom array adapter to customize the item display in
ListView.
P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.
1. Normal ListView example
In this example, we show you how to display a list of fruit name via ListView, it should be easy and self-explanatory.
1.1 Android Layout file
File : res/layout/list_fruit.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:textSize="20sp" > </TextView>
1.2 ListView
package com.mkyong.android; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class ListFruitActivity extends ListActivity { static final String[] FRUITS = new String[] { "Apple", "Avocado", "Banana", "Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit", "Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // no more this // setContentView(R.layout.list_fruit); setListAdapter(new ArrayAdapter<String>(this, R.layout.list_fruit,FRUITS)); ListView listView = getListView(); listView.setTextFilterEnabled(true); listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // When clicked, show a toast with the TextView text Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); } }); } }
1.3 Demo

2. Custom ArrayAdapter example
In this example, we show you how to create 4 items in the ListView, and use a custom “ArrayAdapter” to display different images base on the “item name” in the list.
2.1 Images
Get 4 images for demonstration.

2.2 Android Layout file
File : res/layout/list_mobile.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" > <ImageView android:id="@+id/logo" android:layout_width="50px" android:layout_height="50px" android:layout_marginLeft="5px" android:layout_marginRight="20px" android:layout_marginTop="5px" android:src="@drawable/windowsmobile_logo" > </ImageView> <TextView android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+id/label" android:textSize="30px" > </TextView> </LinearLayout>
2.3 Custom ArrayAdapter
Create a class extends ArrayAdapter and customize the item display in the getView() method.
package com.mkyong.android.adaptor; import com.mkyong.android.R; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; public class MobileArrayAdapter extends ArrayAdapter<String> { private final Context context; private final String[] values; public MobileArrayAdapter(Context context, String[] values) { super(context, R.layout.list_mobile, values); this.context = context; this.values = values; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.list_mobile, parent, false); TextView textView = (TextView) rowView.findViewById(R.id.label); ImageView imageView = (ImageView) rowView.findViewById(R.id.logo); textView.setText(values[position]); // Change icon based on name String s = values[position]; System.out.println(s); if (s.equals("WindowsMobile")) { imageView.setImageResource(R.drawable.windowsmobile_logo); } else if (s.equals("iOS")) { imageView.setImageResource(R.drawable.ios_logo); } else if (s.equals("Blackberry")) { imageView.setImageResource(R.drawable.blackberry_logo); } else { imageView.setImageResource(R.drawable.android_logo); } return rowView; } }
2.4 ListView
ListView, but use above custom adapter to display the list.
package com.mkyong.android; import com.mkyong.android.adaptor.MobileArrayAdapter; import android.app.ListActivity; import android.os.Bundle; import android.widget.ListView; import android.widget.Toast; import android.view.View; public class ListMobileActivity extends ListActivity { static final String[] MOBILE_OS = new String[] { "Android", "iOS", "WindowsMobile", "Blackberry"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new MobileArrayAdapter(this, MOBILE_OS)); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { //get selected items String selectedValue = (String) getListAdapter().getItem(position); Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show(); } }
2.5 Demo








Hi,
1.I want o know how to display a tablet screen with the image and the name, when click on the listview item.Want to do for large screen and small screen tablets with two separate layouts.
2.Also want to know how to Design additional layouts for portrait and landscape versions of app for both tablet and a handset.
3.Also show thumbnails along with their names.
4.Implement navigation from one page to another without going back to the list with ” buttons
5.Implement swipe gesture support to move between the pages, too.
sonali
[...] http://www.mkyong.com/android/android-listview-example/ Vào link này ?? tìm hi?u c? b?n v? ListView nhé! Ph?n c?a b?n thì file layout ph?i có 1 cái <ListView /> và 1 <TextView />, activity c?a b?n có th? extends Activity ho?c extends luôn ListActivity! N?u dùng Activity thì ph?i tìm th?ng listview = (ListView)findViewById(R.id.listViewId) r?i s? d?ng listview.setAdapter(adapter) ?? t?o view cho listview. N?u b?n mu?n click vào Item ? ListView mà TextView thay ??i, thì b?n dùng hàm listview.setOnItemClickListener ?? b?t s? ki?n cho vi?c click vào t?ng item c?a listview! Trả lời với trích dẫn [...]
Sorry Could you help me to solve my trouble.
When I finish codeing, the program can’t import ?
import com.mkyong.android.R;
import com.mkyong.android.adaptor.MobileArrayAdapter;
thank you.
Hi sora,
Don’t import the project in eclipse or which your using.Try to do it.because some projects cant be import. Or you may try this deleted the import statements like
“import com.mkyong.android.R;
import com.mkyong.android.adaptor.MobileArrayAdapter;”
and then try I think it will works for you.
I have parsed a xml and set the value to list using custom adapter.now how to get the value of particular row after clicking on it.
Awesome! This is the best, cleanest implementation I’ve seen yet and I’ve been searching for awhile for this. Thanks!!
please sir,
solution my other problem,
AudioManager accept in other class but MediaPlayer is not accept in other class
please sir,
my problem is one class function call not other class.
ex.
one class…….
{
on create()…..{}
public void hello()
{
Toast.m………….;
}
}
other class…..
{
one class object;
on create(…….)
{
button.seton…….;
onclicklister(…)
{
object.hello();
}
}
}
Superb tutorial,got exact requirement which i need,but one thing …can u plz tel me how to set background image for the list View.If i set that background image for the layout itself its giving for each and every list item separately.But i need sigle image totally as background.
Hello mkyong,
its a good tutorial but how can we do this with database i have tried with database it retriving data but not the images.please give me suggestions how to do with database.
[...] first I saw this tutorial ( http://www.mkyong.com/android/android-listview-example/ ), but realized that it was not what I needed.. because.. it extends Activity… to [...]
Hello mkyong,
Very good tutorial. Thanks a lot.
I have a question.
How to change the background of entire List Item?
Many Thanks
Deepak
Refer link http://stackoverflow.com/questions/2217753/changing-background-color-of-listview-items-on-android
Thanks,
Gubs
Deepak,
In the listView object call the method setBackgroundColor and pass the color you want to set for the view.
If you want to set color for ever click onItemClick method use below code :
Thanks,
Gubs