views:

885

answers:

2

Hey all. I'm trying to use an imageview as a button, and I want to be able to change the image whenever the button's pressed. I have an OnClickListener set, but what do I have to do about when the user's finger is no longer down, over the button? How do I revert to the original image?

+1  A: 

User this instead:

View.OnTouchListener
abstract boolean onTouch(View v, MotionEvent event)

the event parameter will let you know if its ACTION_DOWN or ACTION_UP

Prashast
+3  A: 

Hello,

The correct way of doing this is to extend Button class or if you only want to change the button image you can set a style by xml.

<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal" />
    <item android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/btn_default_normal_disable" />
    <item android:state_pressed="true" 
        android:drawable="@drawable/btn_default_pressed" />
    <item android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/btn_default_selected" />
    <item android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal" />
    <item android:state_focused="true"
        android:drawable="@drawable/btn_default_normal_disable_focused" />
    <item
         android:drawable="@drawable/btn_default_normal_disable" />
</selector>
Lucas S.