Question

I want to change image of an Image Button on hold and change it back on release. Here is my primary code help to edit this. xml codes:

<ImageButton
                android:layout_width="62dp"
                android:layout_weight="1"
                android:layout_height="60dp"
                android:id="@+id/im1"
                android:layout_gravity="fill_horizontal"
                android:src="@drawable/off1"/>

and java codes:

im1 = (ImageButton) findViewById(R.id.im1);
        im1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
          //some codes
          }
        });
Was it helpful?

Solution

You just need to set selector of button in your layout file.

<Button
     android:id="@+id/button1"
     android:background="@drawable/selector_xml_name"
     android:layout_width="200dp"
     android:layout_height="126dp"
     android:text="Hello" />

and done.

Edit

Following is button_effect.xml file in drawable directory

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/numpad_button_bg_selected" android:state_selected="true"></item>
    <item android:drawable="@drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
    <item android:drawable="@drawable/numpad_button_bg_normal"></item>

</selector>

In this, you can see that there are 3 drawables, you just need to place this button_effect style to your button, as i wrote above. You just need to replace selector_xml_name with button_effect.

OTHER TIPS

Yes you have to use selector. you Can use TogggleButton and make it android:checked = "true" and set the background as a @drawable/selector then only on hodiing and releasing it will change its state. toggle_button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/image1_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/image2_pressed"
          android:state_checked="true" />
    <item android:drawable="@drawable/image1_pressed"
          android:state_checked="false" />
</selector>

Then in layout file

<ToggleButton
        android:id="@+id/btnToggle"
        android:layout_height="wrap_content"
        android:layout_marginTop="wrap_content"
        android:textOn=""
        android:textOff=""
        android:checked="true"
        android:background="@drawable/toggle_button_selector" />

same thing you can do with a button also.

use selector like this and apply this drawable in button :

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/buttonbluepressed" />
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/buttonbluepressed" />
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/buttonbluepressed" />
    <item android:drawable="@drawable/buttonblue"/>
</selector>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top