Search code examples
androidandroid-layoutandroid-edittextselectordrawable

Selector for a drawable within an EditText


I have an EditText with a drawableEnd. I want to change this drawable when it's pressed. I've created selector but no image is visible now. Is it even possible to do in this way or maybe I'm doing something wrong?

Layout

<EditText     
    android:drawableEnd="@drawable/selector"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"/>

Selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@drawable/image_on"
        android:state_enabled="true"
        android:state_pressed="true"/>

    <item
        android:drawable="@drawable/image_off"
        android:state_enabled="true"/>

</selector>

Solution

  • Your selector seems to be wrong, You have to declare one statement in one item. AND you set two items for state_enabled="true". It needs to be:

     <item
       android:drawable="@drawable/pressed_image"
       android:state_pressed="true"/>
    
    <item
        android:drawable="@drawable/image_on"
        android:state_enabled="true"/>
    
    <item
        android:drawable="@drawable/image_off"
        android:state_enabled="false"/>
    

    And, I found out since a long time, that also the order of the statements is important. Calling a wrong order will lead into no result sometimes....