Declare this selector in drawables and name it, for example: button.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:drawable="@drawable/btnPressed"/> <item android:drawable="@drawable/btnNormal"></item> </selector>
android: drawable can be color, image, other ... And then you can declare your button as:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button" />
If you create your buttons in code, you can call the method: setBackgroundResource () and pass the resource identifier. Example:
Button button = new Button(this); button.setBackgroundResource(R.drawable.button);
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Emran source share