Button for changing the visual state of a click

Is there a way to animate a button on Android so that when you click on it, it changes the background of the button to the pressed image?

I use only the background property to show the image on the form button.

+7
source share
4 answers

Use this XML: save it in a portable folder and set it as your wallpaper.

<?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false" android:drawable="@drawable/category_listing_bg_img" /> <item android:state_pressed="true" android:drawable="@drawable/category_listing_bg_img_pressed" /> </selector> 
+20
source

add the xml file to your name res / drawable folder it button_selector.xml add also two drawable for the pressed state, and the other for the unset or normal state. Finally, add these two xml file button switches and everything should work! don't forget to set @ drawable / bytton_selector.xml as the background of your button in the main.xml file.

  <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_pressed" android:state_pressed="true"/> <item android:drawable="@drawable/btn_unpressed"/> </selector> 
+5
source

Yes there is. Deploy onTouchListener. use the MotionEvent variable (say, an event) in the onTouch method, write this:

 if (event.getAction() == MotionEvent.ACTION_DOWN){ /*Code*/ } if (event.getAction() == MotionEvent.ACTION_UP){ /*Code*/ } 
+1
source

what you need to do is create a selector (as Krishnakant Dalal talked about). it conveys what the user interface element looks like in each individual state, which can be (pressing, disconnecting, normal, etc.).

Read more about selectors here: http://android-journey.blogspot.com/2009/12/android-selectors.html

+1
source

All Articles