Can You help me please. I need to change background color of my list view item which is selected manually by setSelection(int pos) function and I need to stay with new color until new setSelection call. I have read some topics of how to do this, but I still have no succes. Thanks!
Asked
Active
Viewed 2.8k times
5
-
Try to use a `listSelector` as [here](http://stackoverflow.com/q/2183447/1051783) – gunar Jan 21 '14 at 09:02
-
Check this answer: http://stackoverflow.com/questions/19953285/android-listview-item-background-change – Hulk Jan 21 '14 at 09:03
-
Is your item selected by a click? If it is you can use onItemClick from your listView – Francois Morier-Genoud Jan 21 '14 at 09:05
-
No, I am selecting my item manually by setSelection(int pos) function. – bukka.wh Jan 21 '14 at 09:23
-
Are your list items objects? – Francois Morier-Genoud Jan 21 '14 at 12:40
-
@user1885632 : check this http://stackoverflow.com/questions/19395782/custom-adapter-selected-item-background/19395952#19395952 – Kaushik Jan 21 '14 at 12:59
-
exehs, yes my list items are objects and I use my custom adapter, which extends BaseAdapter class. – bukka.wh Jan 21 '14 at 15:38
-
Effective solution in 2 lines: http://stackoverflow.com/questions/16976431/change-background-color-of-selected-item-on-a-listview/37248223#37248223 – Taochok May 16 '16 at 07:04
2 Answers
8
I've managed to accomplish that by making several selectors for the different states
first put this in your listview
android:listSelector="@drawable/list_selector"
Then create xml files in drawable to control the diferent states
@drawable/list_selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>
</selector>
@drawable/list_item_bg_normal
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="@color/list_background"
android:endColor="@color/list_background"
android:angle="90" />
</shape>
@drawable/list_item_bg_pressed
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="@color/list_background_pressed"
android:endColor="@color/list_background_pressed"
android:angle="90" />
</shape>
In your ListView Selection
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
view.setSelected(true);
...
}
}
Don't forget to add list_background_pressed and list_background to your values/color.xml or just set the color manually in each file.
And I Believe that when you use setSelection(int pos) that will automatically uset the layout you've set as selectected.
Hope it helps.

T.V.
- 793
- 12
- 33
-
-
-
Thanks! I'll try it! Correct me if I am wrong: function setSelection automatically calls onItemClick method of my list view? – bukka.wh Jan 21 '14 at 10:22
-
Yes, I believe it does. Also check this post if you are not successful http://stackoverflow.com/questions/4800415/android-setselected-in-onitemclick-in-listview – T.V. Jan 21 '14 at 10:46
-
1I did as You said but nothing happens((((. Thanks for link I'll check her. May be it will help)) – bukka.wh Jan 21 '14 at 11:02
0
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (updateview != null) updateview.setBackgroundColor(Color.TRANSPARENT);
updateview = view;
view.setBackgroundColor(Color.CYAN);
}
});