Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify layer list programmatically in Android?

Tags:

android

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 ?

like image 767
Rahul Gupta Avatar asked Dec 26 '13 12:12

Rahul Gupta


1 Answers

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);
like image 158
Kailash Dabhi Avatar answered Nov 06 '22 04:11

Kailash Dabhi