Search code examples
androidlayer-list

Set size for individual layer of layer-list from Java


I am showing 2 images in a layer-list this way. Following is layered_map.xml in drawable

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
           <item android:drawable="@drawable/bgimage" />
           <item android:id="@+id/marker"
                 android:drawable="@drawable/point"
                 android:left="660sp"
                 android:right="210sp"
                 android:top="375sp"
                 android:bottom="1125sp"
           />
    </layer-list>

Everything is fine. I get a big background image and a small layered image on top of it. Except now I would want to do what android:left, android:right, android:top, android:bottom do in xml, from Java This is what I have tried

      LayerDrawable layerDrawable = (LayerDrawable)getResources().getDrawable(R.drawable.layered_map);
      Drawable d = layerDrawable.findDrawableByLayerId(R.id.marker);
    d.setBounds(400, 300, 420, 320);
    //d.invalidateSelf();
    mImageView.setImageDrawable(layerDrawable);

But this does not work. I also read somewhere that it is not possible to set different bounds for different layers. I am also skeptical if what xml is doing in this is exactly done by setBounds. Any help will be really appreciated

Thank You


Solution

  • yeah You can do it using setLayerInset (int index, int l, int t, int r, int b)

    See here

    and need help then ask :)