I have a linear layout in which each row is inflated programatically and I want the rows to behave like the ListView when clicked on. That is, I want the row to highlight in the exact same way/colour that the default ListView does. How would I go about doing this?
5 Answers
Ok I have finally figured out how to do this...basically it is done using a selector like the color selector linked by style except instead of 'color' use a drawable for the states and you can refer to the default list drawable that is used in ListView by this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@android:drawable/list_selector_background" />
</selector>
and using this xml as the background for my View.
All the public default drawables can be found here: http://developer.android.com/reference/android/R.drawable.html
-
Thanks for sharing back once you found it. – Jerry Brady Apr 07 '11 at 03:41
-
6If you're like me and need some background on how to use this kind of selector, read: http://android-journey.blogspot.com/2009/12/android-selectors.html – Eric S. Bullington Jul 28 '13 at 12:37
-
Do you happen to know how to keep the textview selected? I have two fragments 1 a listview and 1 a textview of info. Once an item in the listview is selected the item in the textview changes. Is there a way to keep the selected listitem selected? – ksudu94 Nov 05 '13 at 16:23
I was able to do the same with a text view that I wanted to behave like a list item by using:
<Textview
....
android:background="@android:drawable/list_selector_background"
/>

- 12,508
- 5
- 40
- 37
-
8Good answer. For API 11 and above, use instead `android:background="?android:attr/selectableItemBackground"`. More info [here:](http://stackoverflow.com/questions/5546514/making-a-linearlayout-act-like-an-button) – x-code May 08 '14 at 19:15
-
1..and with appcompat-v7 library you can now use ?attr/selectableItemBackground for backwards compatibility – Egor Nov 27 '14 at 14:55
if you still have a problem with that then please remember that some of UI elements are not clickable (RelativeLayout), so you have to add one more line:
<RelativeLayout
....
android:clickable="true"
...

- 61
- 1
- 4
This might be a good place to start looking.
Although, i would advise you to use the ListView itself, rather than implementing it again.

- 33,375
- 8
- 89
- 89
-
I would use the ListView however I need it NOT to scroll and place it inside a ScrolView and that doesn't seem possible(??)...So I ended up trying to implement my own ListView of sort using a LinearLayout. – Kman Jul 16 '10 at 09:29
-
and thanks or the link...but would it be possible to reference and use the default ListViews selector element rather than create my own? – Kman Jul 16 '10 at 09:31
-
you could possibly search for the XML inside the Android Source Code...and just copy it over? i'm not sure if you refer the existing selector,otherwise. – st0le Jul 16 '10 at 09:44
-
Sorry couldnt figur out which one the ListView uses...anyways I tried creating my own but it doesnt seem to be working but am having problems: I want the whole LinearLayout to highlight so what attribute do I set as the colour selector? I tried using textColour but that doesn't seem to work and background makes it crash since I can only set a drawable or single colour as a background :S – Kman Jul 16 '10 at 10:39
To your listview set property
android:listSelector="@color/test"
and this test color set any transparent color you like. you can create any transparent color by using hex transparent color