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);