Changing background color of ListView items on Android
Solution 1
You have to create a different state drawable for each color you want to use.
For example: list_selector_read.xml
and list_selector_unread.xml
.
All you need to do is set everything to transparent except the android:state_window_focused="false"
item.
Then when you are drawing your list you call setBackgroundResource(R.drawable.list_selector_unread/read)
for each row.
You don't set a listSelector on the ListView at all. That will maintain the default selector for your particular flavor of Android.
Solution 2
This is a modification based on the above code, a simplest code:
private static int save = -1;
public void onListItemClick(ListView parent, View v, int position, long id) {
parent.getChildAt(position).setBackgroundColor(Color.BLUE);
if (save != -1 && save != position){
parent.getChildAt(save).setBackgroundColor(Color.BLACK);
}
save = position;
}
I hope you find it useful
greetings!
Solution 3
Ok, I got it to work like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@color/BackgroundColor" />
<item android:drawable="@color/transparent" />
</selector>
YMMV!
Solution 4
No one seemed to provide any examples of doing this solely using an adapter, so I thought I would post my code snippet for displaying ListViews where the "curSelected" item has a different background:
final ListView lv = (ListView)findViewById(R.id.lv);
lv.setAdapter(new BaseAdapter()
{
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = new TextView(ListHighlightTestActivity.this);
convertView.setPadding(10, 10, 10, 10);
((TextView)convertView).setTextColor(Color.WHITE);
}
convertView.setBackgroundColor((position == curSelected) ?
Color.argb(0x80, 0x20, 0xa0, 0x40) : Color.argb(0, 0, 0, 0));
((TextView)convertView).setText((String)getItem(position));
return convertView;
}
public long getItemId(int position)
{
return position;
}
public Object getItem(int position)
{
return "item " + position;
}
public int getCount()
{
return 20;
}
});
This has always been a helpful approach for me for when appearance of list items needs to change dynamically.
Solution 5
mAgendaListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//view.setBackgroundColor(Color.RED);
for(int i=0; i<parent.getChildCount(); i++)
{
if(i == position)
{
parent.getChildAt(i).setBackgroundColor(Color.BLUE);
}
else
{
parent.getChildAt(i).setBackgroundColor(Color.BLACK);
}
}
Marek Stój
A passionate software developer fascinated by the ever growing influence of the theoretical foundations of computer science on the IT industry.
Updated on March 05, 2021Comments
-
Marek Stój over 3 years
How can I change background color of
ListView
items on a per-item basis. When I useandroid:backgroundColor
in theListView
item layout I can achieve this, however the list selector is no longer visible. I can make the selector visible again by settingdrawSelectorOnTop
to true but then the selector overlays the whole item.Any ideas how to change those background colors and keep the selector?
PS I would rather not change the selector itself.
EDIT: Authors of GMail application have managed to achieve exactly this so it's definitely possible.
-
Marek Stój over 14 yearsUnfortunately this doesn't work. Setting BackgroundDrawable has the same effect as setting BackgroundColor - the selector is drawn first so it's hidden.
-
CommonsWare over 14 yearsChange your
android:drawSelectorOnTop
to befalse
. -
Rohit over 11 yearsit highlights the 2 items at the same time. there is some problem in it.
-
Engineer about 11 years+1 This should be the accepted answer. And
row.setBackgroundResource()
happens either inYourCustomArrayAdapter.getView()
OR else you can forego this altogether by simply settingandroid:background="@drawable/your_list_entry_selector"
on the outermost element ofres/layout/your_list_entry_layout.xml
. -
Rohit Walavalkar over 10 years@Nick Wiggill Thanks for the comment . helped a lot
-
suitianshi over 10 yearsthe background is still in blue even if my finger leaves screen
-
Zapnologica about 10 yearswouldn't mind example of the code that goes into list_selector_read.xml
-
Mohammad Zekrallah over 8 yearsthis is not answering the question asked .. he wants to maintain list selector with a different background color for some rows .. your answer will remove list selector style !
-
OWADVL over 8 years@MohammadZekrallah I guess I'm a naughty boy that will not receive any gifts for Christmas
-
Sathish Kumar J over 7 yearsworks nicely. but after using this code list_seletor effect gone. ie. after using this code , when i click the list_item it's not giving clicking effect
-
Victor Lee over 7 yearsThough it doesn't answer the specific question, served a purpose in my app. Thanks.
-
sonichy almost 7 yearsGreat solution!
-
MandisaW almost 6 years@Zapnologica It's a standard Color State List resource. You can always check the Android source repo online and look at their style resources for insight.
-
MandisaW almost 6 yearsCorrection: A State List Drawable resource is needed here, not a Color State List.