Issue
I want to set the android:id = "@android:id/background"
programmatically.
I try to adapting drawable xml code to java code programmatically.
I wrote other options to code, but couldn't write the android:id = "@android:id/background"
to code.
here is my code. please let me know the way.
xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="@color/colorOrange"/>
<stroke android:color="#fff" android:width="1dp"/>
</shape>
</item>
<item android:id="@android:id/progress">
<scale
android:scaleHeight="10%"
android:scaleWidth="100%" >
<shape android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="@color/colorGreen"/>
<stroke android:color="@color/colorGreen"
android:width="4dp"/>
</shape>
</scale>
</item>
</layer-list>
code
val layer1 = GradientDrawable()
layer1.cornerRadius = 10f
layer1.setStroke(1, Color.WHITE)
layer1.setColor(Color.YELLOW)
val scaleDrawable = GradientDrawable()
scaleDrawable.cornerRadius = 10f
scaleDrawable.setStroke(1, Color.WHITE)
scaleDrawable.setColor(Color.BLUE)
val layer2 = ScaleDrawable(scaleDrawable, Gravity.BOTTOM,1f,0.1f)
val drawableList = arrayOf(layer1, layer2)
val layerList : LayerDrawable = LayerDrawable(drawableList)
this.progressDrawable = layerList
result screen
Solution
You can use #layerList.setId
and set the id by index . Check the code below . I have made few changes in it to match it to xml-drawable
.
val layer1 = GradientDrawable()
layer1.cornerRadius = 10f
layer1.setStroke(1, Color.WHITE)
layer1.setColor(Color.YELLOW)
val scaleDrawable = GradientDrawable()
scaleDrawable.cornerRadius = 10f
scaleDrawable.setStroke(1, Color.WHITE)
scaleDrawable.setColor(Color.BLUE)
val layer2 = ScaleDrawable(scaleDrawable, Gravity.START,1f,0.1f)
val drawableList = arrayOf(layer1, layer2)
val layerList = LayerDrawable(drawableList)
layerList.setId(0,android.R.id.background)
layerList.setId(1,android.R.id.progress)
seekBar.progressDrawable = layerList
Answered By - ADM
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.