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.
i think you problem is not the selector file.
you have to add
to your image buttons.
by default the onClick is handled at the listitem level (parent). and the imageButtons dont recieve the onClick.
when you add the above attribute the image button will receive the event and the selector will be used.
check this POST which explains the same for checkbox.
what you are trying to do is more a segmentedbutton than an imagebutton list.
here http://blog.bookworm.at/2010/10/segmented-controls-in-android.html is an example on how to do so. The basic idea is to customize RadioButton instead of ImageButton, since the RadioButton will have the checked state you need
Hi try the following code it will be useful to you,
@ingsaurabh's answer is the way to go if you are using an onClick even. However, if you are using an onTouch event, you can select the different backgrounds (still the same as @ingsaurabh's example) by using
view.setPressed()
.See the following for more details: "Press and hold" button on Android needs to change states (custom XML selector) using onTouchListener