cofudayffy
cofudayffy

Reputation: 43

Change shape in layer-list programatically

i have this drawable file :

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:left="6dp"
        android:right="6dp"
        android:top="6dp"
        android:bottom="6dp"

        >
        <shape android:shape="oval">
            <size android:width="170dp"
                android:height="170dp"/>
            <solid android:color="@color/primaryColor"/>
        </shape>
    </item>

    <item
        android:left="18dp"
        android:right="18dp"
        android:top="18dp"
        android:bottom="18dp">

        <shape android:shape="oval" >
            <size android:width="140dp"
                android:height="140dp"/>
            <solid android:color="#EEEEEE"/>
        </shape>
    </item>


    <item
        android:left="30dp"
        android:right="30dp"
        android:top="30dp"
        android:bottom="30dp">

        <shape android:shape="oval" >
            <size android:width="140dp"
                android:height="140dp"/>
            <solid android:color="@color/primaryColor"/>
        </shape>
    </item>


</layer-list>

and i want to change the shape of only one oval programatically but i found nothing do to this.I want to change only one shape not all at the same time.

I made lot of research but nothing explain how to do this.

So please help me

Upvotes: 0

Views: 466

Answers (1)

Sushant
Sushant

Reputation: 1874

Well, you can use Gradient drawable to achieve this first of all give ID to your layer list items as

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/item1"
    android:left="6dp"
    android:right="6dp"
    android:top="6dp"
    android:bottom="6dp"

    >
    <shape android:shape="oval">
        <size android:width="170dp"
            android:height="170dp"/>
        <solid android:color="@color/primaryColor"/>
    </shape>
</item>

<item
    android:id="@+id/item2"
    android:left="18dp"
    android:right="18dp"
    android:top="18dp"
    android:bottom="18dp">

    <shape android:shape="oval" >
        <size android:width="140dp"
            android:height="140dp"/>
        <solid android:color="#EEEEEE"/>
    </shape>
</item>


<item
    android:id="@+id/item3"
    android:left="30dp"
    android:right="30dp"
    android:top="30dp"
    android:bottom="30dp">e 

    <shape android:shape="oval" >
        <size android:width="140dp"
            android:height="140dp"/>
        <solid android:color="@color/primaryColor"/>
    </shape>
</item>
</layer-list>

Then change your shape items like this

LayerDrawable d = (LayerDrawable) context.getResources().getDrawable(R.drawable.your_drawable_file_name);
GradientDrawable shape = (GradientDrawable) d.findDrawableByLayerId(R.id.item1);
shape.setShape(GradientDrawable.RECTANGLE);
//... Do same for item with id item2 and item3 

Upvotes: 1

Related Questions