Custom ListView in Android

ListView is a control that for presenting components in the form of a list. We can insert a TextBox (EditText), TextView (label), Button or other types of components into a ListView.

Today we will create a ListView like the following:

Custom List View

Now we will move to the coding part of this project.

In this project we will use two layouts. First is activity_main.xml and second is list_single.xml.

activity_main.xml

In the activity_main.xml layout we will use a ListView. Initially this ListView is empty. We will insert the ImageView and TextView dynamically into the ListView through coding.

XML code of this activity is the following:

  1. <RelativeLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4. android:layout_width="match_parent"  
  5. android:layout_height="match_parent"  
  6. android:paddingBottom="@dimen/activity_vertical_margin"  
  7. android:paddingLeft="@dimen/activity_horizontal_margin"  
  8. android:paddingRight="@dimen/activity_horizontal_margin"  
  9. android:paddingTop="@dimen/activity_vertical_margin"  
  10. tools:context=".MainActivity" >  
  11.     <ListView>  
  12. android:id="@+id/list"  
  13. android:layout_width="wrap_content"  
  14. android:layout_height="wrap_content" ></ListView>  
  15. </RelativeLayout>  
After using the preceding XML code, our activity will be like the following:

Item

Now we move to the list_single activity.

lsit_single.xml

In this activity we use a TextView and an ImageView. In the TextView we will show the name of the ListView item and in the ImageView we will show the image of the ListView item.

The XML code of this activity is:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TableLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4. android:layout_width="match_parent"  
  5. android:layout_height="match_parent"   
  6. android:background="#ffbbcc">  
  7.     <TableRow>  
  8.         <ImageView  
  9. android:id="@+id/img"  
  10. android:layout_width="50dp"  
  11. android:layout_height="50dp"/>  
  12.         <TextView  
  13. android:id="@+id/txt"  
  14. android:layout_width="wrap_content"  
  15. android:layout_height="50dp" />  
  16.     </TableRow>  
  17. </TableLayout>  
After using the preceding XML code, the list_single activity will look like the following:

Pink window

This activity is looking empty because we don't provide an image resource to the ImageView and the text to the TextView. We will do all that dynamically.

In the res (resource) folder of this project we will provide numbers of images.

Images

We will use all these Images in the ListView.

Source code (Java code) of this project.

In this project we will use a main activity (MainActivity.java) and will also use a class (CustomList.java).

Now I will explain each of the source files. First of all we will explain the CustomList.java class.

The source code of the CustomeList.java class is:

  1. package learn2crack.customlistview;  
  2. import android.app.Activity;  
  3. import android.view.LayoutInflater;  
  4. import android.view.View;  
  5. import android.view.ViewGroup;  
  6. import android.widget.ArrayAdapter;  
  7. import android.widget.ImageView;  
  8. import android.widget.TextView;  
  9.   
  10. public class CustomList extends ArrayAdapter < String > {  
  11.   
  12.     private final Activity context;  
  13.     private final String[] web;  
  14.     private final Integer[] imageId;  
  15.   
  16.     public CustomList(Activity context, String[] web, Integer[] imageId)   
  17.     {  
  18.         super(context, R.layout.list_single, web);  
  19.         this.context = context;  
  20.         this.web = web;  
  21.         this.imageId = imageId;  
  22.   
  23.     }@Override  
  24.     public View getView(int position, View view, ViewGroup parent)  
  25.     {  
  26.   
  27.         LayoutInflater inflater = context.getLayoutInflater();  
  28.         View rowView = inflater.inflate(R.layout.list_single, nulltrue);  
  29.   
  30.         TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);  
  31.   
  32.         ImageView imageView = (ImageView) rowView.findViewById(R.id.img);  
  33.         txtTitle.setText(web[position]);  
  34.   
  35.         imageView.setImageResource(imageId[position]);  
  36.         return rowView;  
  37.   
  38.     }  
  39. }  
Explanation of Code

Code Section 1

In the preceding class we inherit the ArrayAdapter class using CustomList extends ArrayAdapter<String> statement. Here the extend method is used for inheriting a class. Adapter to use is called an ArrayAdapter because the adapter converts an ArrayList of objects into View items loaded into the ListView container.

Code Section 2
  1. public CustomList(Activity context, String[] web, Integer[] imageId)  
  2. {  
  3.     super(context, R.layout.list_single, web);  
  4.     this.context = context;  
  5.     this.web = web;  
  6.     this.imageId = imageId;  
  7. }  
In the preceding code we created a constructor of the CustomList class. This constructor takes 3 parameters. The first one is the name of the activity that invoked this constructor. The second parameter is a string array object. The third parameter is an Integer array object. We used a super method in the constructor. The super keyword in Java is a reference variable that is used to refer to the immediate parent class object. Whenever we create the instance of a subclass, an instance of the parent class is created implicitly, in other words referred to by the super reference variable.

Code Section 3
  1. public View getView(int position, View view, ViewGroup parent)   
  2. {  
  3.     LayoutInflater inflater = context.getLayoutInflater();  
  4.     View rowView = inflater.inflate(R.layout.list_single, nulltrue);  
  5.     TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);  
  6.     ImageView imageView = (ImageView) rowView.findViewById(R.id.img);  
  7.     txtTitle.setText(web[position]);  
  8.     imageView.setImageResource(imageId[position]);  
  9.     return rowView;  
  10. }  
This is an important part of this project. This method is responsible for inserting a view into the ListView control. This method is invoked implicitly by the system. In this method we pass three parameters. The first parameter contains the position of a view in the ListView. The second parameter is an instance of the view and the third parameter is a ViewGroup. A ViewGroup is a special view that can contain other views (called children). The view group is the base class for layouts and view containers.

In this method we use a LayoutInflater object. LayoutInflater is used to dynamically add and remove views using Java code. This class is used to instantiate a layout XML file into its corresponding View objects.

We use an inflate method (View rowView= inflater.inflate(R.layout.list_single, null, true)) in the preceding code. Inflating means to read the XML file that describes a layout (or GUI element) and to create the actual objects that correspond to it and thus make the object visible within an Android app.

Finally we create a TextView and a ImageView . We provide the text to the textview using a web array (txtTitle.setText(web[position])) and provide the source of the image to the Imageview using an imageid array (imageView.setImageResource(imageId[position])).

Now we move to the source code of the MainActivity.java class.

The following is the source code of this MainActivity.java:
  1. import android.os.Bundle;  
  2. import android.view.View;  
  3. import android.widget.AdapterView;  
  4. import android.widget.ListView;  
  5. import android.widget.Toast;  
  6. import android.app.Activity;  
  7.   
  8. public class MainActivity extends Activity   
  9. {  
  10.     ListView list;  
  11.     String[] web =   
  12.     {  
  13.         "Google Plus",  
  14.             "Twitter",  
  15.             "Windows",  
  16.             "Bing",  
  17.             "Itunes",  
  18.             "Wordpress",  
  19.             "Drupal"  
  20.     };  
  21.     Integer[] imageId =   
  22.     {  
  23.         R.drawable.image1,  
  24.         R.drawable.image2,  
  25.         R.drawable.image3,  
  26.         R.drawable.image4,  
  27.         R.drawable.image5,  
  28.         R.drawable.image6,  
  29.         R.drawable.image7  
  30.   
  31.   
  32.     };  
  33.     @Override  
  34.     protected void onCreate(Bundle savedInstanceState)  
  35.     {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.activity_main);  
  38.   
  39.   
  40.         CustomList adapter = new CustomList(MainActivity.this, web, imageId);  
  41.   
  42.         list = (ListView) findViewById(R.id.list);  
  43.         list.setAdapter(adapter);  
  44.   
  45.         list.setOnItemClickListener(new AdapterView.OnItemClickListener()   
  46.         {  
  47.             @Override  
  48.   
  49.             public void onItemClick(AdapterView <? > parent, View view,  
  50.             int position, long id)   
  51.             {  
  52.                 Toast.makeText(MainActivity.this"You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();  
  53.   
  54.             }  
  55.         });  
  56.     }  
  57. }  
Explanation of Code

Code Section 4
  1. String[] web =   
  2. {  
  3.     "Google Plus",  
  4.         "Twitter",  
  5.         "Windows",  
  6.         "Bing",  
  7.         "Itunes",  
  8.         "Wordpress",  
  9.         "Drupal"  
  10. };  
  11. Integer[] imageId =   
  12. {  
  13.     R.drawable.image1,  
  14.     R.drawable.image2,  
  15.     R.drawable.image3,  
  16.     R.drawable.image4,  
  17.     R.drawable.image5,  
  18.     R.drawable.image6,  
  19.     R.drawable.image7  
  20.   
  21.   
  22. };  
In the preceding code section we create two arrays. One is a string type (String[] web) and another is an Integer type (Integer[] imageId). In the String type array we provide the values for TextView and in the Integer type array we provide the image id for the ImageView.

Code Section 5
  1. super.onCreate(savedInstanceState);  
  2. setContentView(R.layout.activity_main);  
  3. CustomList adapter = new CustomList(MainActivity.this, web, imageId);  
  4. list = (ListView) findViewById(R.id.list);  
  5. list.setAdapter(adapter);  
In the preceding code section first we create an object of the CustomList class. We pass three parameters to the CustomList constructor. The first parameter is the current context name, the second parameter is a String array object and the third is an Integer array object.

In the preceding code we use the setAdapter method for the ListView. The setAdapter method is used in the databinding for a ListView. In our code we bind the data returned by the CustomList class.

Code Section 6
  1. list.setOnItemClickListener(new AdapterView.OnItemClickListener()   
  2. {@Override  
  3.     public void onItemClick(AdapterView <? > parent, View view,  
  4.     int position, long id)   
  5.     {  
  6.         Toast.makeText(MainActivity.this"You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();  
  7.   
  8.     }  
  9. });  
In the preceding code we set an OnItemClick event for the ListView. This event will be invoked when someone clicks on an item of a ListView. On the response of the Click event we show the name of the ListView Item using the Toast.makeText() method.

Let us see an example.

Assume we click on the Google Plus option then the output will be the following:

Google Plus

If we click on the Windows option, then the output will be the following:


 

Up Next
    Ebook Download
    View all
    Learn
    View all