How to Change a Layer-List Drawable

How to change a layer-list drawable?

Question: How do I programmatically reference one of the drawables in the layer-list so I can change it to a different drawable?

Here's what I would try:

Step #1: Put android:id attributes on the <item> elements as illustrated in the documentation

Step #2: Cast the getDrawable() result to a LayerDrawable

Step #3: Call mutate() if you do not want to affect anyone else using the Drawable (may be optional)

Step #4: Call setDrawableByLayerId() to set/replace a Drawable for a given layer ID

How to Change Drawable item in Layer List Android Studio

It just a simple mistake just remove bitmap and give a drawable to item ...

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- First assign id to the list item-->
<item android:id="@+id/color">
<shape android:shape="rectangle">
<corners android:radius="10dp"/>
<stroke android:width="2dp"
android:color="#000000" />
<solid android:color="@color/colorAccent" />
</shape>
</item>
<item android:id="@+id/change_flag" android:bottom="2dp" android:drawable="@drawable/tr1" android:gravity="center">
</item>

Change bottom property of item of layer-list Drawable programmatically

After a lot digging i found a solution, though it solved my problem but it is not what i was looking for.

I created a layer-list drawable and changed its items color dynamically.
Here is the code:

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

<item android:id="@+id/item_bottom_stroke" >
<shape android:shape="rectangle">
<solid android:color="#0096FF"/>
</shape>
</item>
<item android:id="@+id/item_navbar_background" android:bottom="1dp" >
<shape android:shape="rectangle">
<solid android:color="#FFFFFF"/>
</shape>
</item>

Following code modifies above drawable at runtime to change its colors.

LayerDrawable layerDrawable = (LayerDrawable) v.getContext().getResources().getDrawable(R.drawable.layer_list_navigation_with_border);
GradientDrawable strokeDrawable = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.item_bottom_stroke);
strokeDrawable.setColor(strokeColor[0]);
GradientDrawable backgroundColor = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.item_navbar_background);
backgroundColor.setColors(bgColor);

posted solution, thought someone might get benefited.

Android: changing a color of a layer-list

This custom_color in rectangle shape must be accessed through an id, say android:id="@+id/shape_rectangle" so define this first in xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/shape_rectangle">
<shape android:shape="rectangle" >
<solid android:color="@color/custom_color" />
</shape>
</item>
<item android:top="-2dp" android:right="-2dp" android:left="-2dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke
android:width="1dp"
android:color="@android:color/white"/>
</shape>
</item>
</layer-list>

then:

    LayerDrawable shapeRectangle = (LayerDrawable) ContextCompat.getDrawable(context, R.drawable.custom_layer);
GradientDrawable gradient = (GradientDrawable) shapeRectangle.findDrawableByLayerId(R.id.shape_rectangle);
gradient.setColor(Color.RED);

replace custom_layer with your drawable's name

Android Layer List Drawable Move Adjust Item Position

I finally choose to modify the SVG image to meet my needs, following is the process:

  1. Go to "https://materialdesignicons.com/" -> find image -> download SVG image.

  2. Go to "https://editor.method.ac/" -> "File" -> "Open SVG..." -> choose SVG file -> "Open".

  3. Adjust canvas width and height on the right panel.

  4. Adjust the positon of the imported SVG image.

  5. "File" -> "Save Image..." -> rename file -> "Download".

  6. Import the SVG image to Android Studio as vector asset.

How do you change the size of an item in a layer-list?

How about adding pixels to the item?

<item
android:drawable="@mipmap/ic_launcher_foreground"
android:gravity="center"
android:top="dimension"
android:right="dimension"
android:bottom="dimension"
android:left="dimension"/>


Related Topics



Leave a reply



Submit