As @pratik said, save this button.xml in your drawing folder
button.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true"> <shape android:shape="rectangle"> <corners android:radius="5dp"/> <solid android:color="#f00"/> <padding android:left="10.0dip" android:top="10.0dip" android:right="10.0dip" android:bottom="10.0dip"/> <stroke android:width="1.0dip" android:color="#222"/> </shape> </item> <item android:state_pressed="false"> <shape android:shape="rectangle"> <corners android:radius="5dp"/> <solid android:color="#f1f1f1"/> <padding android:left="10.0dip" android:top="10.0dip" android:right="10.0dip" android:bottom="10.0dip"/> <stroke android:width="1.0dip" android:color="#222"/> </shape> </item> </selector>
Apply this button as background
<Button android:background="@drawable/button"/>
and in your class file do this:
public void onClick(View v) { pressedButton.setPressed(true); }
so that the red color is stable.
source share