-2

i use this method but it is not worked. in drawable i created selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="false"
        android:drawable="@drawable/reponseclicked">
    </item>
    <item
        android:drawable="@drawable/repons">
    </item>
</selector>

and this my TextView

 <TextView
    android:layout_width="160dp"
    android:layout_height="wrap_content"
    android:id="@+id/imageView8"
    android:text="text"
    android:gravity="center"
    android:layout_above="@+id/imageView5"
    android:background="@drawable/selector"/>
ugur
  • 3,253
  • 3
  • 21
  • 52
Amirouche Zeggagh
  • 2,532
  • 1
  • 17
  • 17
  • Could you please provide an explanation of "not worked"? Till then the question is not fit for SO as it doesn't have an MCVE. – Ashish Ahuja Jul 09 '16 at 16:58

3 Answers3

1

Chnage android:state_pressed="false" to android:state_pressed="true"

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:drawable="@drawable/reponseclicked" />
    <item
        android:drawable="@drawable/repons"/>
</selector>

See this Thread.

Community
  • 1
  • 1
Sohail Zahid
  • 7,738
  • 2
  • 22
  • 34
0

<color name="colorControlHighlight">#B6B6B6</color>

This in your Drawable

<item android:state_pressed="true">

    <shape>
        <solid android:color="@color/colorControlHighlight"/>
    </shape>
</item>

<item>
    <shape>
        <solid android:color="@color/colorControlHighlight"/>
    </shape>
</item>

<item android:drawable="@color/colorPrimary" />

Also This in your Drawable v21 will give you ripple effect

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/colorControlHighlight">

    <item android:id="@android:id/mask" android:drawable="@android:color/white"/>
    <item android:drawable="@color/colorPrimary" />
</ripple>

Your Textview

<TextView
    android:layout_width="160dp"
    android:layout_height="wrap_content"
    android:id="@+id/imageView8"
    android:text="text"
    android:gravity="center"
    android:layout_above="@+id/imageView5"
    android:background="@drawable/selector"
    android:clickable="true"/>
Siddhesh Dighe
  • 2,504
  • 2
  • 11
  • 15
0

TextView is not clickable by default. So first enable them as below.

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
ugur
  • 3,253
  • 3
  • 21
  • 52