Using ListView in Android

In this tutorial you will learn how to create a list using ListView and how to add functionality to the list, such that clicks and long clicks on items of the list trigger some action.

Step 1: Add a ListView to the xml layout file

In the layout file of your activity add the ListView tag. Depending on how you want your list to look like give the attributes layout_width and layout_height appropriate values.
The following code creates a ListView that fills the whole activity screen:

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lvMenu"
        />

ListView XML

Step 2: Create ListView Instance and ListAdapter in Java

Next you need to create a ListView Instance and add the reference to this instance so as to be able to access the ListView using Java code. After that you need to create an ArrayAdapter that will be set to the ListView so that the ListView gets populated.
The following sample code shows this:


lvMenu = (ListView)findViewById(R.id.lvMenu); // ArrayAdapter takes in the context, LayoutFile for each element , and a string array // You could use some default Layout File for the elements or create custom layouts // The elements of string array are used to populate the ListView lvAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,stringArray); // set the adapter created to the ListView lvMenu.setAdapter(lvAdapter);

ArrayAdapter

Step 3: Setting OnItemClickListener

To make the app do something when an item of the list is clicked you need to add the OnItemClickListener to the ListView.
The following sample code shows how to add OnItemClickListener:


lvMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch (position){ case 0: // Add code to do when first item gets clicked Intent nIntent = null; try { nIntent = new Intent(MainActivity.this,Class.forName("com.anuraag.myapplication.NewActivity")); } catch (ClassNotFoundException e) { e.printStackTrace(); } startActivity(nIntent); break; case 1: // Add code to do when second item gets clicked break; case 2: // Add code to do when third item gets clicked break; case 3: // Add code to do when fourth item gets clicked break; } } });

OnItemClickListener

Step 4: Setting OnItemLongClickListener

If the requirement be you can add extra functionality to your ListView such that some event is triggered when an item of the list is long clicked. To do this you need to set an OnItemLongClickListener to your ListView.
The following sample code shows this:


lvMenu.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { switch (position){ case 0: // Add code to do when first item gets long clicked Toast.makeText(MainActivity.this,"First Item Long Clicked",Toast.LENGTH_SHORT).show(); break; case 1: // Add code to do when second item gets long clicked Toast.makeText(MainActivity.this,"Second Item Long Clicked",Toast.LENGTH_SHORT).show(); break; case 2: // Add code to do when third item gets long clicked Toast.makeText(MainActivity.this,"Third Item Long Clicked",Toast.LENGTH_SHORT).show(); break; case 3: // Add code to do when fourth item gets long clicked Toast.makeText(MainActivity.this,"Fourth Item Long Clicked",Toast.LENGTH_SHORT).show(); break; } return true; } });

ItemLongClick

ScreenShot1

ScreenShot2

ScreenShot3

This content has been helpful to you?

Thanks for contributing!

Yes No