Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change the ImageButton's image when it is pressed and released?

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?

like image 682
David Avatar asked Jun 30 '11 06:06

David


2 Answers

create a selector (it is an xml file) put in drawable folder. and in xml five path of that xml instaed of actual image android:background="@drawable/imageselector" or in program also you can get the same using imageview.setBackgroundDrawable(R.drawable.imageselector)

Following is my selector imageselector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_focused="true"
        android:state_pressed="false"
        android:drawable="@drawable/arow_selected" />
    <item
        android:state_focused="true"
        android:state_pressed="true"
        android:drawable="@drawable/arow_selected" />
    <item
        android:state_focused="false"
        android:state_pressed="true"
        android:drawable="@drawable/arow_selected" />
    <item
        android:drawable="@drawable/arow_unselect" />
</selector>
like image 144
Sunil Kumar Sahoo Avatar answered Sep 19 '22 10:09

Sunil Kumar Sahoo


use selector for this...here is a link for this.. http://developer.android.com/reference/android/widget/ImageButton.html

like image 41
Sujit Avatar answered Sep 21 '22 10:09

Sujit