I have a ListView, I would like that each time i select an item in the listview, the background color changes to grey. If I select another element I want previous elements background to revert to white and the new item's background to be highlighted in grey
Asked
Active
Viewed 829 times
1
-
2In Android, "select" with a `ListView` specifically means "use the up/down arrow keys, D-pad buttons, trackball, etc. to move a highlight bar". Is that what you mean by "select" in your question? – CommonsWare Mar 06 '15 at 13:34
-
When I say select, I mean that the Selected Item's background colour is grey or the background is highlighted in grey – George Mar 06 '15 at 13:36
-
Take a look here : http://stackoverflow.com/questions/19953285/android-listview-item-background-change – Bubu Mar 06 '15 at 13:36
-
That does not answer my question. "Select" (through a 5-way navigation device, like a D-pad) is different than "tap" (through the touchscreen) or "click" (which can be done by either input method). In your question, you used the verb "select". Do you really mean that you are interested solely in the use of a 5-way navigation input device? Or do you really mean "click"? – CommonsWare Mar 06 '15 at 13:39
-
Sorry i wasn't specific. I mean on the onclick – George Mar 06 '15 at 13:40
-
check my answer [here](http://stackoverflow.com/questions/28816813/circle-button-in-android) and instead of button you use it in your listview – Kostas Drak Mar 06 '15 at 13:43
-
To have a `ListView` row stay highlighted after a click, you can use the "activated" pattern. Set up a `StateListDrawable` with a `android:state_activated="true"` state as in http://stackoverflow.com/a/26991860/115145, use that as the row background, and set `android:choiceMode="singleChoice"` on the `ListView`. – CommonsWare Mar 06 '15 at 13:46
-
I used the code below but the listview background is not changing colour :
-
Check this accepted post (http://stackoverflow.com/questions/28359679/highlight-the-selected-textview-in-different-colour/28361532#28361532), and just change `TextView` to be your item `convertView` – Xcihnegn Mar 06 '15 at 13:58
1 Answers
0
If you just want list items to change background color based on selection, that sounds like basic selector functionality. Create a selector xml file and set it as the background in your row xml.
Something like:
< selector xmlns:android="http://schemas.android.com/apk/res/android">
< item android:drawable="@android:drawable/myGreyColor" android:state_pressed="true"/>
< item android:drawable="@android:drawable/myGreyColor" android:state_selected="true"/>
< item android:drawable="@android:drawable/myWhiteColor"/>
< /selector>

Lance Toth
- 430
- 3
- 17