Issue
Having a round button in xml, I want to change its color when clicking on it. I do it with this code, but it changes to square shape again, not to the original round shape:
button1.setBackgroundColor(Color.BLUE);
Does anyone know how I can change the button's shape as well?
Solution
GradientDrawable shape = new GradientDrawable();
shape.setCornerRadius( 8 );
// add some color
// You can add your random color generator here
// and set color
if (i % 2 == 0) {
shape.setColor(Color.RED);
} else {
shape.setColor(Color.BLUE);
}
// now find your view and add background to it
findViewById( R.id.my_view ).setBackground(shape);
Answered By - Raj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.