Android how to make View highlight when clicked?

30,118

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

Share:
30,118
Kman
Author by

Kman

Updated on November 20, 2020

Comments

  • Kman
    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
    Kman almost 14 years
    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
    Kman almost 14 years
    and thanks or the link...but would it be possible to reference and use the default ListViews selector element rather than create my own?
  • st0le
    st0le almost 14 years
    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.
  • Kman
    Kman almost 14 years
    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
  • Jerry Brady
    Jerry Brady about 13 years
    Thanks for sharing back once you found it.
  • Eric S. Bullington
    Eric S. Bullington almost 11 years
    If 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
    Atomix over 10 years
    This is the best solution. Thanks
  • ksudu94
    ksudu94 over 10 years
    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?
  • x-code
    x-code about 10 years
    Good answer. For API 11 and above, use instead android:background="?android:attr/selectableItemBackground". More info here:
  • Egor
    Egor over 9 years
    ..and with appcompat-v7 library you can now use ?attr/selectableItemBackground for backwards compatibility