Android Custom Listview with Alternative Background for List Item

By | July 24, 2013

In the previous series of tutorial we have seen the example of Simple ListView and Custom ListView , In this tutorial we will see how to create ListView with alternative background color for list item or row

Android Custom ListView With Alternative Item Background Color

1. XML Layout

  Create XML File
    1)  list_activity.xml

file:- list_activity.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:orientation=”horizontal” >

    <TextView
        android:id=”@+id/label”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_marginLeft=”20dp”
        android:layout_marginTop=”15dp”
        android:text=””
        android:textAllCaps=”true”
        android:textSize=”20dp” >
    </TextView>

</LinearLayout>

2. Activity

    Create Activity Class
       1) MainActivity.java

file:- MainActivity.java

package com.example.ListView_AltRow;

import android.os.Bundle;
import android.app.ListActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

 String[] mob = new String[] { “Item 0”, “Item 1”, “Item 2”, “Item 3”,
   “Item 4”, “Item 5”, “Item 6”, “Item 7” };

 @Override
 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  Custom_view cv = new Custom_view(this, mob);
  setListAdapter(cv);
 }

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {

  super.onListItemClick(l, v, position, id);

  // getting the value of clicked item
  String clicked_item = (String) getListAdapter().getItem(position);
  Toast.makeText(this, “You clicked : ” + clicked_item,
    Toast.LENGTH_SHORT).show();
 }
}

3. Custom Adapter

   Create Class
         1)Custom_view.java

file:- Custom_view.java

package com.example.ListView_AltRow;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.com.pavan.customized_listview.R;

public class Custom_view extends ArrayAdapter<string> {

 private final Context context;
 private final String[] values;

 public Custom_view(Context context, String[] values) {
  super(context, R.layout.list_activity, values);
  this.context = context;
  this.values = values;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  LayoutInflater inflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  View rowView = inflater.inflate(R.layout.list_activity, parent, false);
  TextView tv = (TextView) rowView.findViewById(R.id.label);
  String item_value = values[position];

  tv.setText(item_value);

  if (position % 2 == 0) {
   rowView.setBackgroundColor(Color.parseColor(“#ffffff”));
  } else {
   rowView.setBackgroundColor(Color.parseColor(“#BCF7F0”));
  }
  return rowView;
 }
}