5

I have an issue with my ImageButton not changing state. When I click, or rather touch, the button it stays as the same image. Here is the XML I am using as a selector.

<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
 android:state_focused="true"
 android:state_pressed="false"
 android:drawable="@drawable/pushed" />
<item
 android:state_focused="true"
 android:state_pressed="true"
 android:drawable="@drawable/pushed" />
<item
 android:state_focused="false"
 android:state_pressed="true"
 android:drawable="@drawable/pushed" />
<item  
 android:drawable="@drawable/default" />
</selector>

I call this selector from my main.xml as

android:background="@drawable/imagechoice"

imagechoice.xml is the file with the selector

I don't understand why this is not working, unless I have to have some java code, but everything I've seen said this should work.

Adam Wagner
  • 15,469
  • 7
  • 52
  • 66
Doug Goldberg
  • 51
  • 1
  • 2
  • Are you solve this problem? I have the same unresolved problem http://stackoverflow.com/questions/19435660/imagebutton-selector-not-working – sagus_helgy Oct 18 '13 at 05:04

5 Answers5

2

When using an ImageButton, isn't it the 'src' property you should use and not background?

David Brown
  • 3,021
  • 3
  • 26
  • 46
2

Make sure that you copy the same images and the button XML into every "drawable" folders (hdpi,ldpi,mdpi). That's how I solved this problem on my app.

Good luck :)

eLLe
  • 21
  • 1
1

I have nearly the same XML and it works just fine. Are you sure you're not replacing the drawable in code somewhere?

On another note, your XML can be simplified by using the cascading nature of the state matching.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/pushed"
          />
    <item android:state_focused="true"
          android:drawable="@drawable/pushed"
          />
    <item android:drawable="@drawable/default"
          />
</selector>
Matt Briançon
  • 1,064
  • 16
  • 24
0

Make sure you set Image button background as mentioned below.I think you are not setting the selector as background instead you are setting the image as background.

 <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_up_selector"
        android:text="1"
        android:textColor="#fffafa"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="5dp"/>
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
0

This is my xml of a button with my own custom image on it and it works great:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/btn_off" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/btn_pressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/btn_pressed" />
<item android:drawable="@drawable/btn_off" />
</selector>
justinl
  • 10,448
  • 21
  • 70
  • 88
  • are you using an IDE such as Eclipse? Maybe clean the project to make sure the R files refresh themselves and double check that the art is the correct art. I'm not really sure why you're code isn't working. – justinl Dec 26 '10 at 21:19
  • I am using Eclipse, ill try to do that with the R files. Thanks – Doug Goldberg Dec 27 '10 at 15:47