Android Dynamic Build button style

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.