In this example we’ll create a listview with a custom list divider, alternate backgrounds for list items and a list selector.
At first let’s create a listview and give them necessary attributes.
res/layout/main.xml
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000">
</ListView>
List Divider
Now Let’s customize our listview with a simple list divider.All we need to do is just give divider and dividerHeight, XML attributes to our listview. The list divider is a Drawable with our custom shapes and color. In this example, we will use Gradient Drawable as a list divider list_divider.xml
res/drawable/list_divider.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="270.0"
android:endColor="#f6fafc"
android:startColor="#c9dbe4" />
</shape>
Add this list divider to our listview by adding android:divider="@drawable/list_divider"
to ListView tag. Also, give height to our divider by adding tag android:dividerHeight="1dp"
Alternate backgrounds to ListView
For adding your alternate backgrounds, you can create your custom adapter, and add your changes to the method getView()
. In this example, i’ve created MyListAdapter that extends ArrayAdapter
and two ColorDrawables
as my background colors that’ll be added to list items.
Color Drawable 1 : res/drawable/list_background1.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#e9f1f4" />
</shape>
Color Drawable 2 : res/drawable/list_background2.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#f6f9fa" />
</shape>
MyListAdapter.java
public class MyListAdapter extends ArrayAdapter {
int[] listItemBackground = new int[] { R.drawable.list_background1,
R.drawable.list_background2 };
public MyListAdapter(Context context, int resource, int textViewResourceId,
String[] objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
// your list item layout to inflate here
}
int listItemBackgroundPosition = position % listItemBackground.length;
view.setBackgroundResource(listItemBackground[listItemBackgroundPosition]);
return view;
}
}
List Selectors
After adding background to the list item, we’ll see no selectors for our list. In this example i’ve created a list selector for pressed_state.
res/drawable/list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_selector_pressed" android:state_pressed="true"></item>
</selector>
Color Drawable for pressed state : res/drawable/list_selector_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#bfced4" />
</shape>
Now add list selector attribute android:listSelector
to the ListView tag. Since our list item background is on the top of list selector we have to modify the background drawables we’ve used in get view.For the pressed state we’ll set transparency to our list item backgrounds. So, create color background with different selection states.
Color Drawable 1 : res/drawable/fancy_list_background1.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/transparent" android:state_pressed="true"></item>
<item android:drawable="@drawable/list_background1"></item>
</selector>
Color Drawable 2 : res/drawable/fancy_list_background2.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/transparent" android:state_pressed="true"></item>
<item android:drawable="@drawable/list_background2"></item>
</selector>
Now replace the values in listItemBackground with these drawables.
int[] listItemBackground = new int[] { R.drawable.fancy_list_background1,
R.drawable.fancy_list_background2 };
Our final ListView will be
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:divider="@drawable/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" >
</ListView>