1. First define a Shape.xml file in the Drawable folder, which reads as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<shape
Xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:shape= "Rectangle" >
<solid android:color= "#e6e6"/>
<corners
Android:topleftradius= "10DP"
Android:toprightradius= "10DP"
Android:bottomrightradius= "10DP"
android:bottomleftradius= "10DP"/>
<padding
android:left= "30DP"
android:top= "0DP"
android:right= "30DP"
Android:bottom= "0DP"
/>
</shape>
2. In the Main.xml file, the button uses this shape.xml, as follows:
<button
Android:id= "@+id/button_next"
android:layout_height= "Wrap_content"
Android:layout_width= "Match_parent"
android:text= "@string/button_next"
Android:textsize= "@dimen/label_text_size"
Android:layout_alignparentbottom= "true"
Android:layout_centervertical= "true"
Android:gravity= "Center_horizontal|center_vertical"
android:layout_marginleft= "15DP"
android:layout_marginright= "15DP"
Android:background= "@drawable/shape"
/>
At this point, the button displays the shape defined as shape.xml.
3. Dynamically modify the button background color in your code as follows:
Gradientdrawable Bgshape = (gradientdrawable) buttonnext.getbackground ();
Bgshape.setcolor (Color.Blue);
Android button, XML file definition shape, change background color in code