1

I have the following xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
  <item>
    <shape
      android:shape="rectangle">
      <stroke android:width="1dp" android:color="#000000" />
      <solid android:color="#000000" />
    </shape>
  </item>

  <item android:top="0dp" android:bottom="1dp">
    <shape
      android:shape="rectangle">
      <stroke android:width="1dp" android:color="#414141" />
      <solid android:color="#414141" />
    </shape>
  </item>

  <item android:top="1dp" android:bottom="1dp">
    <shape
      android:shape="rectangle">
      <stroke android:width="1dp" android:color="#2C2C2C" />
      <solid android:color="#2C2C2C" />
    </shape>
  </item>
</layer-list>

This does exactly what I want but I can't use it. I want to change the colors of the three layers on runtime so I have to do this completely programmatically. I know I have to use LayerDrawable but I don't know how to implement the equivalent of android:top and android:bottom. Can somebody help me?

anel
  • 173
  • 1
  • 3
  • 10
  • Found the answer. Just use setLayerInset of the LayerDrawable. See http://stackoverflow.com/questions/12427025/android-gradient-drawable-programmatically – anel Feb 15 '13 at 13:37
  • You should write the answer you found as a real answer to your question and then accept that as an answer. – Simon Forsberg Nov 02 '13 at 16:07

1 Answers1

0
ImageView imageView1 = (ImageView) findViewById(R.id.imgView1);
    ((GradientDrawable ) imageView1.getBackground()).setColorFilter(
            Color.RED,  PorterDuff.Mode.SRC_ATOP);
luttu android
  • 1,443
  • 16
  • 22