Dynamically generated button styles are used:
int bordercolor = Color.parsecolor ("#2E3135"); int bgColor = Color.parsecolor ("#00FF00"); Sets the view background style with border width, border color, number of rounded angles, and background color. gradientdrawable shape = Drawableutils.createshape (1, 4, bordercolor, BgColor); Btn1.setbackground (Shape); int textnormalcolor = Color.parsecolor ("#2E3135"); int textpresscolor = Color.parsecolor ("#FF0000"); int textfocuscolor = Color.parsecolor ("#00FF00"); int textenablecolor = Color.parsecolor ("#0000FF"); Set text click style Toggle Colorstatelist colorstatelist = Drawableutils.createcolorstatelist (Textnormalcolor, TextPressColor, Textfocuscolor, Textenablecolor); Btn2.settextcolor (colorstatelist); /* android:focusableintouchmode= "true" control gets focus by default */int color1 = Color.parsecolor ("#FF0000"); int color2 = Color.parsecolor ("#00FF00"); Set view click style Toggle Gradientdrawable unselected = Drawableutils.createshape (1, 4, Color1, Color2); Gradientdrawable selected = Drawableutils.createshape (1, 4, Color2, Color1); Statelistdrawable Statelistdrawable = drawableutils.createstatelistdrawable (unselected, selected); Btn3.setbackground (statelistdrawable);
Tool class:
public class Drawableutils {/** * Creates a shape * * @param strokewidth Border Width (px) * @param roundradius Corner radius ( PX) * @param strokecolor border color * @param fillcolor inner Fill Color * @return gradientdrawable */public static G radientdrawable createshape (int strokewidth, int roundradius, int strokecolor, int fillcolor) {gradientdrawable GD = new Gradientdrawable (); Gd.setcolor (FillColor); Gd.setcornerradius (Roundradius); Gd.setstroke (Strokewidth, Strokecolor); return GD; }/** * Create button text Click Style * * @param Normal style * @param pressed press style * @param focused focus style * @param Unable unavailable style * @return Colorstatelist */public static colorstatelist createcolorstatelist (int normal, int pr essed, int focused, int unable) {int[] colors = new int[]{pressed, focused, normal, focused, unable, normal}; Int[][] states = new int[6][]; States[0] = new int[]{android. R.attr.state_pressed, Android. R. attr.state_enabled}; STATES[1] = new int[]{android. R.attr.state_enabled, Android. R.attr.state_focused}; STATES[2] = new int[]{android. R.attr.state_enabled}; STATES[3] = new int[]{android. R.attr.state_focused}; STATES[4] = new int[]{android. R.attr.state_window_focused}; STATES[5] = new int[]{}; return new Colorstatelist (states, colors); }/** * Create button click Style * * @param unselected no click Style * @param selected tap style * @return statelistdrawable */public static statelistdrawable createstatelistdrawable (gradientdrawable unselected, gradientdrawable selected) { Statelistdrawable drawable = new statelistdrawable (); Drawable.addstate (New int[]{android. r.attr.state_pressed}, selected); Drawable.addstate (New int[]{-android. R.attr.state_pressed}, unselected); return drawable; }}
Android dynamically generated button styles