What is ListView in Android?

A ListView is an android widget that allows us to render a list of scrollable items.

ListView is an adapterview like gridview and spinner.

This means that it requires an adapter for it to insert its items. The adapter becomes responsible for pulling data from a content source.

Android ListView

This source can be an array or something more complex like database or from the network.

Not only that but the adapter will also be responsible for converting each item result into a view that will be placed into the listview.

This is because as an adapterview the ListView does not know the details, such as type and contents, of the views it contains.

So it will ask for the views on demand from a ListAdapter as needed. For instance it asks for these views as the user scrolls up or down.

Each of the views in the ListView is positioned immediately below the previous view in the list.

ListView API Definition

Here’s ListView’s API definition.

   ↳    android.view.View
       ↳    android.view.ViewGroup
           ↳    android.widget.AdapterView<android.widget.ListAdapter>
               ↳    android.widget.AbsListView
                   ↳    android.widget.ListView

Clearly you can see ListView is residing in the android.widget package and deriving from the abstract AbsListView class.

How do we Display a ListView?

Well to display a ListView all you need is add the ListView in the XML layout.

      android_layout_height="match_parent" />
  1. android:id="@+id/list_view" – We are assigning the ListView an ID.
  2. android:layout_width="match_parent" – We set the width of our ListView to match that of the Layout onto which the ListView is being rendered.
  3. android:layout_height="match_parent" – We set the height of our ListView to match that of the Layout onto which the ListView is being rendered.
Setting ItemClicks to Simple ListView.

Here’s how we will listen to itemClicks for our ListView, thus showing a simple toast message.

We need to invoke the setOnItemClickListener() method of our ListView and pass into it an AdapterView.OnItemClickListener() annonymous class, and then override the onItemClick() method.

In this case pos is the position of the clicked item in the ListView.

        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {
                Toast.makeText(MainActivity.this, languages.get(i), Toast.LENGTH_SHORT).show();

How do we create a Custom Adapter for ListView.

You can use several adapters, but the most commonly used is BaseAdapter.

private class MyCustomAdapter extends BaseAdapter {

      // override other abstract methods here

      public View getView(int position, View convertView, ViewGroup container) {
          if (convertView == null) {
              convertView = getLayoutInflater().inflate(R.layout.my_list_item, container, false);

          ((TextView) convertView.findViewById(
          return convertView;

How do we set an adapter to a ListView?

We set an adapter to a ListView using the setAdapter() method.

This method is as follows:

public void setAdapter (ListAdapter adapter)

The setAdapter() method is responsible for binding our data to our ListView.

We need to pass an adapter to this method. This adapter may be wrapped by a WrapperListAdapter, depending on the ListView features currently in use. For instance, adding headers and/or footers will cause the adapter to be wrapped.

A WrapperListAdapter is a List adapter that wraps another list adapter.

Adapter ListAdapter: The ListAdapter which is responsible for maintaining the data backing this list and for producing a view to represent an item in that data set.

Which XML attributes are defined in the ListView class?

Majority of the XML attributes you will use are defined in the parent classes like AbsListView, which also inherits from other base classes like ViewGroup and View.

However ListView also has the following attributes defined:

No. Attribute Description
1. android:divider Drawable or color to draw between list items.
2. android:divider Drawable or color to draw between list items.
3. android:dividerHeight Height of the divider.
4. android:entries Reference to an array resource that will populate the ListView.
5. android:footerDividersEnabled When set to false, the ListView will not draw the divider before each footer view.
6. android:headerDividersEnabled When set to false, the ListView will not draw the divider after each header view.

Which types of Adapters can ListView be used with?

Here are some of the adapters that can be used with a ListView:

No. Method Definition
1. BaseAdapter A super class of common implementations for an android.widget.Adapter interface.It implements both ListAdapter and SpinnerAdapter interfaces.
2. ArrayAdapter A BaseAdapter child which uses an array of arbitrary objects as data source.
3. CursorAdapter An abstract BaseAdapter child used to expose data from android.database.Cursor to a ListView.

Simple ListView Example

Let’s look at a simple ListView example that will handle display a list of programming languages.

When the user clicks a single item, the clicked language is shown in a Toast message.



Share an Example

Share an Example

What is the capital of Egypt? ( Cairo )