I have a layer list set as a background to a Textview :-
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Bottom 2dp Shadow -->
<item>
<shape android:shape="rectangle" >
<solid android:color="@android:color/black" />
<corners android:radius="15dp" />
</shape>
</item>
<!-- Blue Top color -->
<item
android:bottom="1.5px"
android:right="1px">
<shape android:shape="rectangle" >
<solid android:color="#2E93FA" />
<corners android:radius="15dp" />
</shape>
</item>
<solid android:color="#2E93FA" />
<corners
android:radius="15dp" />
--> I need to show different colors in a listview for this layer list.
I need to modify the Second Item programmatically through code (dynamically). Can anyone help ?
Have a look at the LayerDrawable
because it is created from your XML and used at runtime.
Example:
my_drawable.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/gradientDrawble">
<shape android:shape="rectangle" >
<gradient
android:endColor="#897343"
android:startColor="#345456" />
<corners android:radius="15dp" />
</shape>
</item>
Modify at runtime:
LayerDrawable layerDrawable = (LayerDrawable) getResources()
.getDrawable(R.drawable.my_drawable);
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable
.findDrawableByLayerId(R.id.gradientDrawble);
gradientDrawable.setCornerRadius(50);
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