Android: Button/Image Pressed Effect


To add Button/Image pressed effect you will have to design two images first. 1st for normal view and 2nd for clicked view.

  1. Design two images, 1 for normal view and other for clicked view in res\ drawable (i am naming 1st ‘button_normal.png’ and second ‘button_selected.png’).
  2. Create a new layout in res\layout,(i am naming it as ‘button_style.xml’).
  3. Place following code into new layout i.e. in button_style.xml.
  4. <?xml version="1.0" encoding="UTF-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    	<item android:drawable="@drawable/button_normal"/><!-- Normal Button View -->
    	<item android:state_pressed="true" android:drawable="@drawable/button_selected"/><!-- Pressed/Tapped Button View -->
    </selector>
    
  5. Your button view states have been created, you will now need to assign it to any button or image.
  6. Create a button in your Activity Layout and assign layout ‘button_style’ to its background.
  7. <Button 	android:background="@layout/button_style"
    		android:text="My Button"
    		android:id="@+id/my_btn" >
    </Button>
    
  8. In same way this effect can be applied to an Image.
  9. Enjoy.