ekjyot
ekjyot

Reputation: 2227

changing image of imagebutton till it is pressed

I want that in my android application, the ImageButton change its image when it is pressed and released, and when it is pressed released again, the image for ImageButton will be changed back , how to do that? I have tried it with selector. But it is not working. Please anyone help me. Thanks

Upvotes: 3

Views: 724

Answers (2)

Yashwanth Kumar
Yashwanth Kumar

Reputation: 29121

use the following selector.

<?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/pressedImage"/>
  <item 
    android:state_focused="true"
    android:drawable="@drawable/normalImage"/>
  <item android:drawable="@drawable/normalImage"/>
</selector>

Upvotes: 2

Nikunj Patel
Nikunj Patel

Reputation: 22066

make file in drawable folder a.xml ::

<?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/oneImage" />
  <item
    android:state_pressed="false"
    android:drawable="@drawable/secondImage" />
</selector>

and in your main file :: button have implment following code ::

<ImageButton android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="anything"
        android:id="@+id/first"
        android:background="@drawable/a"
        android:textColor="#FFFFFF" 
        android:textStyle="bold"
        ></ImageButton>

Upvotes: 5

Related Questions