Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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">
    <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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top