3
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <shape>
        <solid
                android:color="#FF0000" />
    </shape>
</item>

<item
    android:id="@android:id/progress">
    <clip>
        <shape>
            <solid
                android:color="#00FF00" />
        </shape>
    </clip>
</item>

</layer-list>

This Drawable I am applying on Seekbar. I have to access this Drawable in Code and change its "background"/"progress" Color.

But really I don't have any idea that how can I access it in a Code.

Please help me to do this.

EDIT

LayerDrawable ld = (LayerDrawable) getResources().getDrawable(R.drawable.my_drawable);
ClipDrawable cd = (ClipDrawable) ld.getDrawable(1);
cd.setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
Amit Yadav
  • 32,664
  • 6
  • 42
  • 57
  • This is what you looking for http://developer.android.com/reference/android/graphics/drawable/LayerDrawable.html – gnuanu Jul 24 '14 at 13:29

2 Answers2

1

Try this:

For fragment: getActivity().getResources().getDrawable(R.drawable.<your_drawable>);

For activity: getResources().getDrawable(R.drawable.<your_drawable>);

Hope this helps.

luiscosta
  • 855
  • 1
  • 10
  • 16
0

I don't believe you can alter the actual background color of the drawable from code, but you could apply a colorFilter:

Drawable drawable = getResources().getDrawable(R.drawable.your_drawable);
                drawable.setColorFilter(cf)
erik
  • 4,946
  • 13
  • 70
  • 120