Making Custom Android ListViews: Part 2


Apr. 13

5.19 K


android_developer_tutorialContinuing our discussion from our discussion from last Tuesday we are going resume creating custom listviews for our android apps. Last time we sorted out the visual elements of the listviews or the appearance of the ListView and today we are going to program the behavior of the ListViews.

Get a Cup of Java In…

Starting off we create a new java class. For example here we are going to make the menu of a weather forecasting app so we are going to name our class This class is going to be our Custom Array Adapter and is going to hold the objects which we will bind to our ListView.

[code language=”java”]
public class Weather {
public int icon;
public String title;
public Weather(){

public Weather(int icon, String title) {
this.icon = icon;
this.title = title;

Getting the Adapter

Now going back to the views that we created in our last post listview_item_row.xml has two views corresponding to the attributes of Well you guessed correct: the properties of are going to be displayed in those views. Which begs the question: how do we connect them?  This is where the CustomArrayAdapter comes in. This is going to connect our class to the views. It is going to inherit Android ArrayAdapter class. Only difference is that we are going to override getView method. Quite naturally we are going to make a separate class called WeatherAdapter. Have a look at how to implement it.

[code language=”java”]
public class WeatherAdapter extends ArrayAdapter<Weather>{

Context context;
int layoutResourceId;
Weather data[] = null;

public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context; = data;

public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;

if(row == null)
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);

holder = new WeatherHolder();
holder.imgIcon = (ImageView)row.findViewById(;
holder.txtTitle = (TextView)row.findViewById(;

holder = (WeatherHolder)row.getTag();

Weather weather = data[position];

return row;

static class WeatherHolder
ImageView imgIcon;
TextView txtTitle;

In the above code,the first important thing is the constructor of the class that takes three parameters.

  • The first parameter is the Context and we can pass the reference of the activity in which we will use WeatherAdapterclass.
  • The second parameter is the resource id of the layout file we want to use for displaying each ListView item. We will pass the resource id of our layout file listview_item_row.xml for this parameter.
  • The third parameter is an array of Weather class objects that will be used by the Adapter to display data.

Next the parent class getView method is overridden. This method will be called for every item in the ListView to create views with their properties set as we want. The getView method is also using a temporary holder class declared inside the WeatherAdapter class. This class will be used to cache the ImageView and TextView so they can be reused for every row in the ListView and this will provide us a great performance improvement as we are recycling the same two views with different properties and we don’t need to find ImageView and TextView for every ListView item. The above code is also using the Android built in Layout Inflator to inflate (parse) the xml layout file.

Lets Get This Puppy On Its Way

Now that we got that out of the way we can now add the final touches to main activity. Ours here is the generic

[code language=”java”]
public class MainActivity extends Activity {

private ListView listView1;

public void onCreate(Bundle savedInstanceState) {

Weather weather_data[] = new Weather[]
new Weather(R.drawable.weather_cloudy, "Cloudy"),
new Weather(R.drawable.weather_showers, "Showers"),
new Weather(R.drawable.weather_snow, "Snow"),
new Weather(R.drawable.weather_storm, "Storm"),
new Weather(R.drawable.weather_sunny, "Sunny")

WeatherAdapter adapter = new WeatherAdapter(this,
R.layout.listview_item_row, weather_data);

listView1 = (ListView)findViewById(;

View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);


There are few things in the MainActivity that you need to better understand:

  • First notice, we are creating an array of Weather class objects and icons and titles are passed as constructor parameters.
  • Next, WeatherAdapter object is created and listview_item_row.xml layout file resource id and Weather objects array is passed in its constructor.

Once again, we are using Android Layout Inflator to inflate our listview_header_row.xml layout file and once it is parses as header View it is passed as a parameter of ListView addHeaderView method. Finally we are passing our custom adapter to ListView setAdapter method.

Once you have done this you have finished everything properly and successfully implemented your first Custom Android ListView. With our icons it looks somewhat like this.

You can see this in action in most of our works of our Android Developers on the Play Store.



Customizing Android ListView Items with Custom ArrayAdapter by Waqas Anwar


Lets Nurture
Posted by Lets Nurture


Have an !dea or need help with your current business?

We use cookies to give you tailored experiences on our website.