Custom ArrayAdapter, with different icons.

Posted: May 15, 2013 in Android Posts, Android Tutorials

In the last exercises “A simple ListView, extends ListActivity”, “ListView, with icon” and “Implement onListItemClick() of ListActivity”, all use the build-in ArrayAdapter with standard layout in each row. All row have the same icon.

In this article, a custom ArrayAdapter will be created. The method getView() have to be re-implement. Such that we can have a List with different icons on each row.

In this exercise, we will show original icon come from Project Wizard for Sunday, and a gray icon for all others.

Custom ArrayAdapter, with with different icons.

Create folder /res/drawable and save the gray icon inside.

Keep usng the /res/layout/row.xml as previous exercise, “ListView, with icon”.

package com.exercise.AndroidList;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidList extends ListActivity {

public class MyCustomAdapter extends ArrayAdapter<String> {

public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
TextView label=(TextView)row.findViewById(;
ImageView icon=(ImageView)row.findViewById(;

if (DayOfWeek[position]=="Sunday"){

return row;

String[] DayOfWeek = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
   /*setListAdapter(new ArrayAdapter<String>(this,
     R.layout.row,, DayOfWeek));*/
   setListAdapter(new MyCustomAdapter(AndroidList.this, R.layout.row, DayOfWeek));

protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
//super.onListItemClick(l, v, position, id);
String selection = l.getItemAtPosition(position).toString();
Toast.makeText(this, selection, Toast.LENGTH_LONG).show();


It’s a programmatic pitfall here:
in getView(), Condition checking of String (“Sunday”) should be checked with:


Download the files.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s