Note that there are some explanatory texts on larger screens.

plurals
  1. POStateListDrawable with LayerDrawable created programatically not working on Android 4.0+
    primarykey
    data
    text
    <p>I am trying to create an option group with coloured squares for the user to choose one. It is working on a 3.2 device and looking like in this picture: <img src="https://i.stack.imgur.com/Y8Ak9.png" alt="enter image description here"></p> <p>The code is something like:</p> <pre><code>for (int i = 0; i &lt; COLORS.length; i++) { CheckBox box = new CheckBox(context); box.setBackgroundDrawable(getColorOption(context, COLORS[i])); box.setButtonDrawable(android.R.color.transparent); </code></pre> <p>And then, in the getColorOption function I create the StateListDrawable:</p> <pre><code> StateListDrawable slDrawable = new StateListDrawable(); LayerDrawable checkedDrawable = new LayerDrawable(new Drawable[] { new SelectFrameShapeDrawable(transparentColor, lightRedColor), new SquareShapeDrawable(color) }); LayerDrawable uncheckedDrawable = new LayerDrawable(new Drawable[] { new SelectFrameShapeDrawable(transparentColor, transparentColor), new SquareShapeDrawable(color) }); slDrawable.addState(new int[] { android.R.attr.state_checked }, checkedDrawable); slDrawable.addState(new int[] { -android.R.attr.state_checked }, uncheckedDrawable); return slDrawable; </code></pre> <p>The SquareShapeDrawable class is a ShapeDrawable:</p> <pre><code>public class SquareShapeDrawable extends ShapeDrawable { private final Paint fillpaint; public SquareShapeDrawable(int color) { super(new RectShape()); fillpaint = new Paint(this.getPaint()); fillpaint.setColor(color); } @Override protected void onDraw(Shape shape, Canvas canvas, Paint paint) { shape.draw(canvas, fillpaint); } } </code></pre> <p>And the SelectFrameShapeDrawable is:</p> <pre><code>private class SelectFrameShapeDrawable extends ShapeDrawable { private final Paint fillpaint, strokepaint; public SelectFrameShapeDrawable(int fill, int stroke) { super(new RectShape()); strokepaint = new Paint(this.getPaint()); strokepaint.setStyle(Paint.Style.STROKE); strokepaint.setStrokeWidth((int) (getResources() .getDisplayMetrics().density + 0.5f)); strokepaint.setColor(stroke); int padding = (int) (4 * getResources().getDisplayMetrics().density + 0.5f); setPadding(padding, padding, padding, padding); fillpaint = new Paint(strokepaint); fillpaint.setColor(fill); } @Override protected void onDraw(Shape shape, Canvas canvas, Paint paint) { if (strokepaint != null) shape.draw(canvas, strokepaint); shape.draw(canvas, fillpaint); } } </code></pre> <p>On a 4.2 device all the squares are black and do not change when checked: <img src="https://i.stack.imgur.com/rNHW3.png" alt="enter image description here"></p> <p>The problem seems to be when adding the drawables to the StateListDrawable... Any idea how to solve this? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload