Android: How to change the size of a GradientDrawable within a layer-list programmatically
<p>I have a layer-list within a file layers.xml:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;layer-list xmlns:android="http://schemas.android.com/apk/res/android"&gt;
&lt;item android:id="@+id/outer_rect"&gt;
    &lt;shape android:shape="rectangle"&gt;
        &lt;stroke
                android:width="3dp"
                android:color="@color/gray4"/&gt;
        &lt;solid android:color="@color/white"/&gt;
    &lt;/shape&gt;
&lt;/item&gt;
&lt;item android:top="10dp" android:bottom="10dp" android:left="10dp" android:right="10dp"
      android:drawable="@drawable/dot_pattern_bitmap"/&gt;
&lt;/layer-list&gt;
</code></pre>

<p>This is used in a customized FrameLayout customView.xml:</p>

<pre><code>&lt;merge xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    &lt;ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/layers"/&gt;
&lt;/merge&gt;
</code></pre>

<p>which itself is used e.g. in</p>

<pre><code>&lt;FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/space_m"
    android:orientation="vertical"&gt;

&lt;my.namespace.customView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/&gt;

&lt;/FrameLayout&gt;
</code></pre>

<p>Additionally, I have the CustomView.java class to edit the behavior of the customView (and especially the layers).</p>

<p>In there, I am trying to achieve an update of the size of the layer-list depending on the size of the customView (and a parameter 'ratio'). I tried it like this:</p>

<pre><code>// ...
@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    layoutWidth = MeasureSpec.getSize(widthMeasureSpec);
    layoutHeight = MeasureSpec.getSize(heightMeasureSpec);

    readjustSize(ratio);
}

private void readjustSize(final double ratio) {
    if (layoutHeight != 0 &amp;&amp; layoutWidth != 0) {
        // determine desired width and height using the layout width
        int desiredWidth = layoutWidth;
        int desiredHeight = (int) (layoutWidth / ratio);

        // if it does not fit, adjust using the layout height
        if (desiredHeight &gt; layoutHeight) {
            desiredHeight = layoutHeight;
            desiredWidth = (int) (layoutHeight * ratio);
        }

        // get the basic layer
        final LayerDrawable ld =
            (LayerDrawable) ContextCompat.getDrawable(getContext(), R.layers);
        final GradientDrawable outerRectShape =
            (GradientDrawable) ld.findDrawableByLayerId(R.id.outer_rect);

        // and adapt the size
        outerRectShape.setSize(desiredWidth, desiredHeight);
        outerRectShape.setBounds(0, 0, desiredWidth, desiredHeight);

        // redraw
        invalidate();
        requestLayout();
    }
}
</code></pre>

<p>So, that was lots of code... Unfortunately, this does not work properly. If the frame layout is loaded the first time, I can only see a very small rectangle. </p>

<p><a href="https://i.stack.imgur.com/Etqcf.png" rel="nofollow"><img src="https://i.stack.imgur.com/Etqcf.png" alt=""></a></p>

<p>Only if I switch back and forth to this view, the size is applied as I wanted to:</p>

<p><a href="https://i.stack.imgur.com/b20kz.png" rel="nofollow"><img src="https://i.stack.imgur.com/b20kz.png" alt=""></a></p>

<p>Does anybody have ideas?</p>