Download Code

Android ListView Example

In android listview example you will learn how to use ListView in android application.In Previous tutorial you have learnt to create List View programmatically using ListActivity.So,now you will learn using some xml code.You can say it’s a custom listview.

ListView

List View is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter.

Array Adapter

A concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.

Create new android listview example application project.Drag and Drop List View in activity_main.xml file.

android custom listview,android custom listview example,custom listview example,listview example

Create new layout item.xml and place TextView.

android custom listview,android custom listview example,custom listview example,listview example

Go to your MainActivity.java class.

  • Create variable of ListView.
  • Create String array.
  • Refer id of List View.
  • store string array in ArrayAdapter.
  • Set ArrayAdapter to List View.
  • Apply click event on list view items.
  • Display Toast to show which item is clicked.
package developer.listview;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class MainActivity extends Activity{

	ListView codecList;   // list view variable
	// string array for list menu
	String androidCodec[] = {"Java","C#","C++","Php","Javascript","Android","Codec"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        codecList = (ListView) findViewById(R.id.list);   // refering list view of xml
        //setting array adapter with item layout
    	ArrayAdapter adapter = new ArrayAdapter(this, R.layout.item,R.id.itemTxt, androidCodec); 
        codecList.setAdapter(adapter);   // setting adapter to list view
        // click event on list view items
        codecList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();

			}
		});
        
    }
    
   
}

Now run your android listview example application project.It will display a list of array which you initialized at the start.When you click on any list item.It will display that item text in Toast.

android custom listview,android custom listview example,custom listview example,listview example,Android ListView example

that’s it for this tutorial.

Thanks.