How to set two different color for odd row and even row of a ListView programmatically?

by / Wednesday, 14 August 2013 / Published in Android

In a Listview two different color can be set for odd row and even row.By creating a custom adapter  and by extending the ArrayAdapter and overriding  the getView method it can be handled. And inside the getView method based on the condition   the color of the text  can be changed using  setBackgroundcolor attribute.

let see the code inside  Adapter class  getview()  method:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater=LayoutInflater.from(context);
View view=inflater.inflate(R.layout.list, null);
textView=(TextView)view.findViewById(R.id.text_hint);
imageView=(ImageView)view.findViewById(R.id.img);

if(position%2==0)
{
view.setBackgroundColor(Color.parseColor("#F4FA58"));
textView.setTextColor(Color.BLACK);
}else
{
view.setBackgroundColor(Color.parseColor("#DA81F5"));
}
return view;

}

 

TOP