To add Button/Image pressed effect you will have to design two images first. 1st for normal view and 2nd for clicked view.
- 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’).
- Create a new layout in res\layout,(i am naming it as ‘button_style.xml’).
- Place following code into new layout i.e. in button_style.xml.
- Your button view states have been created, you will now need to assign it to any button or image.
- Create a button in your Activity Layout and assign layout ‘button_style’ to its background.
- In same way this effect can be applied to an Image.
<?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>
<Button android:background="@layout/button_style" android:text="My Button" android:id="@+id/my_btn" > </Button>