Reusing an Edge Resource with Multiple Background Colors

I currently have an xml file in the / res / drawable folder:

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

<solid
    android:color="@color/transparent_white" />

<stroke
    android:width="1dip"
    android:color="@color/light_gray" />
</shape>

Sometimes I would like the background to be white, gray, blue, etc. depending on the item I'm trying to install. Is there a way to do this without creating n number of xml files, where the only difference is the color of the solid attribute?

+4
source share
1 answer

you can do this by dynamically declaring the form and changing the color at runtime.

ShapeDrawable shapeDrawable= new ShapeDrawable();
shapeDrawable.setShape(new RectShape());
shapeDrawable.getPaint().setColor(<your color>);

((TextView) row.findViewById(<your viewid>)).setBackgroundDrawable(shapeDrawable);
0
source

Source: https://habr.com/ru/post/1570016/


All Articles