I have a LinearLayout which I would like to set a background programmatically. This background should be very simple, just two rectangles one next to each other. The width of the rectangles is only known at runtime. What would be the best way?
ShapeDrawable done = new ShapeDrawable(new RectShape());
done.setBounds(new Rect(0, 0, 0, 0));
done.getPaint().setColor(Color.GREEN);
ShapeDrawable remaining = new ShapeDrawable(new RectShape());
remaining.setBounds(new Rect(20, 0, 0, 0));
remaining.getPaint().setColor(Color.RED);
LayerDrawable composite = new LayerDrawable(new Drawable[]{remaining, done});
weightRow.setBackgroundDrawable(composite);
I have tried to create a compound drawable like this, where I've expected the remaining rectangle to start at position 20, but it just fills the whole layout.
LayerDrawablemuch/in a while, but from what I remember, drawable containers likeLayerDrawablemay maintain or consolidate state among all the drawables in that container (like bounds).