0

I have made three different images for a next button in my activity. A focused image, normal image and an image when the button is not enabled.

However, I wanted to test it and see how ot looks when it's not been enabled.

So on start I set it to false and it does work, I cannot touch it and it does not change to focused anymore, but the image does not change.

The ImageButton from my layout.

    <ImageButton
        android:id="@+id/nextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:padding="5dp"
        android:src="@drawable/nextbutton" />

The selector file nextbutton.xml

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

    <item android:drawable="@drawable/btn_next_focused" android:state_focused="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_next_focused" android:state_focused="false" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_next_focused" android:state_focused="true"/>
    <item android:drawable="@drawable/btn_next" android:state_focused="false" android:state_pressed="false"/>
    <item android:drawable="@drawable/btn_next_disabled" android:state_enabled="false"/>

</selector>

What is wrong? It'll only change to focused and normal state but never to the disabled one. (I just noticed that it might be the wrong word for it...).

Davlog
  • 2,162
  • 8
  • 36
  • 60

1 Answers1

1

I found it out. I still don't know for sure why but I have a clue. I had to this line to every other item in the selector.

android:state_enabled="false"

So it looks like this :

<item android:drawable="@drawable/btn_next_focused" android:state_focused="true" android:state_pressed="true" android:state_enabled="false" />
<item android:drawable="@drawable/btn_next_focused" android:state_focused="false" android:state_pressed="true" android:state_enabled="false" />
<item android:drawable="@drawable/btn_next_focused" android:state_focused="true" android:state_enabled="false" />
<item android:drawable="@drawable/btn_next" android:state_focused="false" android:state_pressed="false" android:state_enabled="false" />
Davlog
  • 2,162
  • 8
  • 36
  • 60
  • Great tip, solved my issue. After all its quite simple: Android searches for a matching selector from top to bottom. First one (the default with no states defined) always matches and is always used... – crysxd Aug 16 '16 at 12:34