Sponser

create your own banner at mybannermaker.com!

Wednesday, March 23, 2011

Customized ListView item's selection color can be changed on touching or selecting the item in the list

On selection, on focus, on touch of the items we can change the color of the item, by default android will show a color, we can change it as we like. For that we need this xml to be stored inside the color folder, here only we need mention the colors for each listeners




list_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="@color/grey" />
<item android:state_pressed="true"
android:drawable="@color/blue" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@color/blue" />
</selector>



That list_bg.xml will be called in the customized list xml as background


list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_margin="2dp" android:background="@color/list_bg">

<TextView android:id="@+id/post" android:gravity="center_vertical"
android:layout_width="wrap_content" android:layout_height="50dp"
android:textSize="20sp" android:textColor="#D0640D"
android:layout_toRightOf="@+id/bite_image" />

</RelativeLayout>


This one is the main xml for the Lister.java


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">

<ListView android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:smoothScrollbar="true"
android:background="#fff" android:cacheColorHint="#fff"
android:fastScrollEnabled="false" android:clickable="true"
android:layout_marginBottom="36dp" />

</LinearLayout>



The Lister.java will be like this, the main thing is the list_bg.xml only


Lister.java

package com.list.viewer;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class Lister extends ListActivity{

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] captionArray = { "USA","India","England","Russia","Europe","Canada","Srilanka","Singapore","Thailand","Australia"};
ItemsAdapter ItemsAdapter = new ItemsAdapter(
Lister.this, R.layout.list,
captionArray);
setListAdapter(ItemsAdapter);
}


private class ItemsAdapter extends BaseAdapter {
String[] items;

public ItemsAdapter(Context context, int textViewResourceId,
String[] items) {
// super(context, textViewResourceId, items);
this.items = items;
}

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

View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list, null);
}
TextView post = (TextView) v
.findViewById(R.id.post);
post.setText(items[position]);

return v;
}

public int getCount() {
return items.length;
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}
}
}





The full source code

Download source


Have a good day.

10 comments:

  1. Hi, if I change background of view in getView method, is there anyway to have background colors for both normal and highlighted states as shown in your screenshots(normal: white, highlight: blue)?

    Thanks!

    ReplyDelete
  2. What can i do if i want to create even click on any list. For example, after i click Europe, there is a Picture appear.
    Thanks before hand for your answers.

    ReplyDelete
  3. Simple, straight, good!

    Thanks! Best example I've found so far!

    ReplyDelete

Sponser