Search code examples
androiddynamicpropertiesbasic4android

Can a property such as First Color be set using code?


Can a property such as First Color be set using code?

I would like to do something like this:

btnMyButton.drawable = "StatelistDrawable"
btnMyButton.drawable.EnabledDrawable = "GradientDrawable"
btnMyButton.drawable.EnabledDrawable.firstcolor = "255, 199, 199"
btnMyButton.drawable.EnabledDrawable.secondcolor = "255, 79, 79"

Solution

  • If I understand your question correctly you need to create a GradientDrawable as below:

    GradientDrawable gradient = new GradientDrawable( GradientDrawable.Orientation.TOP_BOTTOM, new int[] {0xFFRRGGBB,0xFFRRGGBB}); gd.setCornerRadius(0f);

    Where RRGGBB is the color code in hex (eg 99CC00)

    And then set the drawable as the background of your button:

    btnMyButton.setBackgroundDrawable(gradient);