Programmatically change color of shape in layer list

android layer-list drawable programmatically
android change shape stroke color programmatically
android layer-list background color
change drawable color programmatically android
layer-list tint
layer-list centercrop
android layer-list position
android layer-list size

How can I programmatically change the color (#000000) of a shape in a layer list?

Here is my layer list:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#000000" /> // CHANGE THIS COLOR
        </shape>
    </item>
    <item android:left="5dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/bg" />
        </shape>
    </item>
</layer-list>

First of all you need to assign id to you layer-list item.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- First assign id to the list item-->
    <item  android:id="@+id/your_shape">  
        <shape android:shape="rectangle">
            <solid android:color="#000000" /> 
        </shape>
    </item>
    <item android:left="5dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/bg" />
        </shape>
    </item>
</layer-list>

Then get your shape by id.

LayerDrawable shape = (LayerDrawable) getResources().getDrawable(R.drawable.your_shape)

And you can change the color of your shape by calling

shape.setColor(Color.Black); // changing to black color

EDIT

As getDrawable() has been deprecated. Use the following line of code.

LayerDrawable shape = (LayerDrawable) ContextCompat.getDrawable(YourActivity.this,R.drawable.your_shape)

Changing color in a shape inside a layer-list programmatically , Ok I found the answer, I just had to put the id of the shape inside the item not in the shape kachel_ticked_style.xml: And then you can change either the color of the shape calling shape.setColor or the color of the stroke calling shape.setStroke(strokeWidth,strokeColor) share | improve this answer answered Dec 23 '13 at 8:36

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/gradientDrawble"> // Give id
        <shape android:shape="rectangle">
            <solid android:color="#000000" /> // CHANGE THIS COLOR
        </shape>
    </item>
    <item android:left="5dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/bg" />
        </shape>
    </item>
</layer-list>

Then in you code just add

LayerDrawable layerDrawable = (LayerDrawable) getResources()
            .getDrawable(R.drawable.my_drawable);
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable
            .findDrawableByLayerId(R.id.gradientDrawble);
gradientDrawable.setColor(color); // change color

Update Oct-2016

getDrawable() is now deprecated, you should use ContextCompat.getDrawable(context, color) instead.

Beside, if you get the LayerDrawable by findDrawableByLayerId(), then you had to call view.setBackground(layerDrawable) for this to take effect. Alternatively, instantiating the layerDrawable by view.getBackground() also worked.

programmatically change the solid color in xml, I need to change the background color programmatically. <layer-list <shape android:shape="rectangle"> <solid android:color="#333333"  But still this might set the solid portion of the shape. @TiGer You should add @username in your comment to make sure a notification is sent to the user. By the way, you will need to subclass ShapeDrawable in order to set the stroke portion. More info here: Link.

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

    <item android:id="@+id/outer">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <size android:height="40dp" android:width="40dp"/>
            <padding android:top="3dp" android:bottom="3dp" android:right="3dp" android:left="3dp"/>
            <stroke
                android:width="1dp"
                android:color="#ffffff"/>
        </shape>
    </item>

    <item android:id="@+id/middle">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <size android:height="40dp" android:width="40dp"/>
            <padding android:top="2dp" android:bottom="2dp" android:right="2dp" android:left="2dp"/>
            <stroke
                android:width="3dp"
                android:color="#ffffff"/>
        </shape>
    </item>

    <item android:id="@+id/inner">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <size android:height="40dp" android:width="40dp"/>
            <solid
                android:color="#ffffff"/>
        </shape>
    </item>
</layer-list>

Find your view

LayerDrawable layerDrawable = (LayerDrawable) yourView.getBackground();
GradientDrawable outer = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.outer);
GradientDrawable middle = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.middle);
GradientDrawable inner = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.inner);

Set color

outer.setColor(Color.parseColor(#000000));
inner.setColor(Color.parseColor(#FFFFFF));

android.graphics.drawable.LayerDrawable.findDrawableByLayerId , Change shape solid color at runtime inside Drawable xml used as background How to modify layer list programmatically in Android? LayerDrawable  Set android shape color programmatically. December 16, 2017 - by mhdr - Leave a Comment Change Process Priority in Python . April 16, 2020. dataframe.groupby in

You can also get the drawable or background of the view and set it as follows.

LayerDrawable layerDrawable = (LayerDrawable) view.getDrawable(); //view.getBackground()
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable
                .findDrawableByLayerId(R.id.layer_list_item_id);
gradientDrawable.setColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));

android.graphics.drawable.LayerDrawable java code examples , private void changeBackgroundColor(int color) { LayerDrawable layer = (​LayerDrawable) getBackground(); GradientDrawable shape = (​GradientDrawable) layer . through layers to and change to RoundedDrawables if possible for (int i = 0; i < num; i++) { Drawable d How to modify layer list programmatically in Android? Programmatically overlay the image or drawable with the colour you want. While the purpose of this article is mainly to discuss option 2, option 1 will fulfil most requirements and most developers

Color state list resource, I am trying to change programmatically the color of the selectable_kachel_shape. Here is the xml file: kachel_ticked_style.xml <layer-list  Use a triangle image and a rectangular image and mathematically align them in the above mentioned format. Use color filtering to dynamically change its color. You can even draw them on a custom view, using vector graphics, using custom colors, and that would be another way of solving this problem.

Drawable resources, A ColorStateList is an object you can define in XML that you can apply as a color, but will actually change colors, depending on the state of the  There are three layer in this example. The bottom layer is a blue square which rotate 45 degree rotation. The middle layer is a red oval. The top layer is a green oval. You can adjust each layer’s item offset to change the heart border and shape. If you set same color for both three layer. You can see a heart picture.

Android Layer-List Example, Layer List: A Drawable that manages an array of other Drawables. XML file that defines a geometric shape, including colors and gradients. Change the color of a shape. When you create a shape with a shape tool, the shape takes the color of the present foreground color. To change the color of an existing shape, open the Layers panel by choosing Window→Layers. Notice that the Vector Shape tool creates a new layer for every shape you make.

Comments
  • possible duplicate : stackoverflow.com/questions/20705992/…
  • Suhail Mehta gave a detail answer. And also explain how to set single item shape color. It should be accepted Answer
  • getDrawable() is deprecated.
  • for me it works better if I get the layerlist with getBackground on the widget that has it instead of geetting it from resources
  • This solution didn't work for me, your_shape is an id and not a drawable. Suhail's answer works for me.
  • LayerDrawable drawableFile = (LayerDrawable) getResources().getDrawable(R.drawable.drawable_file_name); GradientDrawable gradientDrawable = (GradientDrawable) drawableFile.findDrawableByLayerId(R.id. your_shape);
  • getDrawable() is deprecated. Is there another way?
  • @user Just use ContextCompat.getDrawable()
  • Any reason why this only works when I restart my Activity (for example, using Instant Run)?
  • This is the correct answer. Another option is gradientDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); where color is a resolved color, for example Color.BLACK
  • This is the one that worked for me. (And I had tried the two other solutions.)