Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a LayerDrawable object programmatically

Tags:

android

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>
like image 747
Gratzi Avatar asked Nov 30 '10 09:11

Gratzi


1 Answers

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

like image 75
waynesford Avatar answered Oct 10 '22 14:10

waynesford