How can I create this drawable programmatically?
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="@drawable/android_red"
android:gravity="center" />
</item>
<item android:top="10dp" android:left="10dp">
<bitmap android:src="@drawable/android_green"
android:gravity="center" />
</item>
<item android:top="20dp" android:left="20dp">
<bitmap android:src="@drawable/android_blue"
android:gravity="center" />
</item>
</layer-list>
This wasn't too obvious until I read the comments under @OctavianDamiean's answer. His hyperlink is broken, but in order to programmatically set an item
's android:top
, android:bottom
, etc., reference LayerDrawable
's setLayerInset
method.
setLayerInset (int index, int l, int t, int r, int b)
int index
- the index of the Drawable
out of the Drawable
array (that you passed in as the LayerDrawable
's constructor parameter) that you want to modify.
int l, t, r, b
- Set these as you would in android:left
, android:top
, etc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With