I want imagebutton with two states i) normal ii) touch(or click).
I have set normal image in the imagebutton background and I am trying to change image(pressed) from onclick method, but it doesn't change.
I want that if I pressed image button then image should change from normal to pressed until I press other button but it doesn't happen.
Can anyone suggest to me how can I do this with selector or at run time?
Here is my imagebuttonpanel code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="bottom"
android:id="@+id/buttonpanel">
<ImageButton android:id="@+id/buttonhome"
android:layout_width="80dp"
android:layout_height="36dp"
android:focusable="true"
android:background="@drawable/homeselector">
</ImageButton>
<ImageButton android:id="@+id/buttonsearch"
android:layout_height="36dp"
android:layout_width="80dp"
android:background="@drawable/searchselector"
android:focusable="true">
</ImageButton>>
<ImageButton android:id="@+id/buttonreg"
android:layout_height="36dp"
android:layout_width="80dp"
android:background="@drawable/registerselector"
android:focusable="true">
</ImageButton>>
<ImageButton android:id="@+id/buttonlogin"
android:layout_height="36dp"
android:layout_width="80dp"
android:background="@drawable/loginselector"
android:focusable="true">
</ImageButton>
</LinearLayout>
and my selector xml is
<?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/homehover"
/> <item android:state_focused="true" android:drawable="@drawable/homehover"
/> <item android:drawable="@drawable/home" /> <item android:state_window_focused="true"
android:drawable="@drawable/homehover" /> -->
<item android:state_enabled="false" android:drawable="@drawable/homehover" />
<item android:state_pressed="true" android:state_enabled="true"
android:drawable="@drawable/homehover" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="@drawable/homehover" />
<item android:state_enabled="true" android:drawable="@drawable/home" />
</selector>
And I have also tried to change image resource on ontouch and onclick event, but it does not help.
Have you tried
CompoundButton
? CompoundButton has the checkable property that exactly matches your need. Replace ImageButtons with these.Change selector xml to the following. May need some modification but be sure to use
state_checked
in place ofstate_pressed
.In
CompoundButton.OnCheckedChangeListener
you need to check and uncheck other based on your conditions.Similarly set a
OnCheckedChangeListener
to each button, which will uncheck other buttons when it is checked. Hope this Helps.if you want pressed image button then image should be change from normal to pressed
But I best way will be to customize the RadioButton and use them in a group. I have see an example of that. Sorry I did not remember that link.
but if you want to avoid that. You need to add this to your selector.xml
Once Done. Just got to your code and add this
You will see the result. But you have to mange the states which button was recently press. So that you can set
I suggest you to put all button reference in a array.
you can create selector file in res/drawable
example: res/drawable/selector_button.xml
and in layout activity, fragment or etc
Try this
Create an xml file in your drawable like this :
And set images accordingly and then set this xml as background of your
imageButton
.mhh. I got another solution which helped me, 'cause I just have two different sates:
In my .xml I define the ImageButton like this:
The corresponding selector file looks like this:
And in my onCreate I call:
done. hope this helps. took me a while to figure out.