Create software-accessible progress

I have a scenario where I need to have a large number of progress indicators. I cannot create xml resources for everyone, because I want the user to select a color, which will then be used to dynamically create drawable. Below is one such option for drawing in xml, how can I create this program for accurate replication?

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <shape> <solid android:color="@color/transparent" /> <stroke android:width="2px" android:color="@color/category_blue_stroke"/> </shape> </item> <item android:id="@android:id/progress"> <clip> <shape> <solid android:color="@color/category_blue" /> <stroke android:width="2px" android:color="@color/category_blue_stroke"/> </shape> </clip> </item> </layer-list> 
+4
source share
1 answer

From the links provided by Rajesh and g00dy, I was able to come up with a solution.

 public static Drawable createDrawable(Context context) { ShapeDrawable shape = new ShapeDrawable(); shape.getPaint().setStyle(Style.FILL); shape.getPaint().setColor( context.getResources().getColor(R.color.transparent)); shape.getPaint().setStyle(Style.STROKE); shape.getPaint().setStrokeWidth(4); shape.getPaint().setColor( context.getResources().getColor(R.color.category_green_stroke)); ShapeDrawable shapeD = new ShapeDrawable(); shapeD.getPaint().setStyle(Style.FILL); shapeD.getPaint().setColor( context.getResources().getColor(R.color.category_green)); ClipDrawable clipDrawable = new ClipDrawable(shapeD, Gravity.LEFT, ClipDrawable.HORIZONTAL); LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] { clipDrawable, shape }); return layerDrawable; } 

This code will create a drawable that is visually similar to what xml creates in my question.

+14
source

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


All Articles