android java set random background colors of shape and round corners

www.lau‮irutt‬.com
android java set random background colors of shape and round corners
GradientDrawable shape =  new GradientDrawable();
// round the corner
shape.setCornerRadius( 8 );

// setting random color
if (i % 2 == 0) {
    shape.setColor(Color.RED);
} else {
    shape.setColor(Color.BLUE);
}

View view = (LinearLayout) findViewById( R.id.my_view );
view.setBackground(shape);
Created Time:2017-10-08 22:09:15  Author:lautturi