ImageButton Change Image OnClick XML

In this post you can learn on how to change the image of the ImageButton OnClick in android. This is very simple and you can use a simple XML file to make this functionality happen.

changeimage.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/button_focused"
          android:state_focused="true" />
    <item android:drawable="@drawable/button_normal" />

</selector>

changeimage is an xml file that we must create inside drawable folder where "button_pressed", "button_focused" and "button_normal" are images under drawable folder respectively.

layout.xml

Make a reference of the above XML file in the button declaration to make it effect.

<Button
        android:id="@+id/imagebutton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:background="@drawable/changeimage" />

No comments:

Post a Comment

My Profile