Listview with Alternative Row colors

Introduction

     This post will help you make a list with alternate row in different colors. To change the background color of a row in ListView you have to use a custom Adapter.so we use custom_view adaper method.

demo


Using the code

activity_main.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="10dp"
android:layout_marginBottom="10dp"
android:text=""
android:textSize="20dp" >
</TextView>

</LinearLayout>


MainActivity.java:
package com.example.listview_alternativecolors;

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

public class MainActivity extends ListActivity 
{
 String[] mob = new String[] { "Chennai""New Delhi""Mumbai""kolkata",
 "Bangalore""Hyderabad""Srinagar","Mysore","Chidambaram",
"Mayiladuthurai","Tiruvarur" };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);

Custom_view cv = new Custom_view(thismob);
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();
}
}


Custom_view.java :
package com.example.listview_alternativecolors;

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;

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.activity_main, 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.activity_main, 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("#A4A4A4"));
else {
rowView.setBackgroundColor(Color.parseColor("#FFBF00"));
}
return rowView;
}
}

                        
Previous
Next Post »

1 comments:

Write comments