Android how to make View highlight when clicked?
Solution 1
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
Solution 2
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"
/>
Solution 3
This might be a good place to start looking.
Although, i would advise you to use the ListView itself, rather than implementing it again.
Solution 4
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"
...
Solution 5
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
Kman
Updated on November 20, 2020Comments
-
Kman over 3 years
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?
-
Kman almost 14 yearsI 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 almost 14 yearsand thanks or the link...but would it be possible to reference and use the default ListViews selector element rather than create my own?
-
st0le almost 14 yearsyou 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.
-
Kman almost 14 yearsSorry 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
-
Jerry Brady about 13 yearsThanks for sharing back once you found it.
-
Eric S. Bullington almost 11 yearsIf you're like me and need some background on how to use this kind of selector, read: android-journey.blogspot.com/2009/12/android-selectors.html
-
Atomix over 10 yearsThis is the best solution. Thanks
-
ksudu94 over 10 yearsDo 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?
-
x-code about 10 yearsGood answer. For API 11 and above, use instead
android:background="?android:attr/selectableItemBackground"
. More info here: -
Egor over 9 years..and with appcompat-v7 library you can now use ?attr/selectableItemBackground for backwards compatibility