Issue
This is my drawable circular shape
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<size
android:width="30dp"
android:height="30dp"/>
<gradient
android:angle="0"
android:startColor="#FF63a34a"
android:endColor="#FF477b36"
android:type="linear"
/>
</shape>
I want to create this layout using drawable circular shape with different colors.

Solution
If you using recyclerview to inflate these items then you can this reference.
You have to take GradientDrawable from your drawable and then mutate your drawable based on your condition & logic. You can create your color array as per your requirement.
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val imageView: ImageView = view.findViewById(R.id.myImage)
fun bind() {
val colors = IntArray(2)
colors[0] = getRandomColor()
colors[1] = getRandomColor()
val backgroundDRw = ResourcesCompat.getDrawable(itemView.context.resources, R.drawable.my_drwable, null) as GradientDrawable
backgroundDRw.mutate()
backgroundDRw.colors = colors
imageView.background = backgroundDRw
}
private fun getRandomColor(): Int {
val rnd = Random()
return Color.argb(255, rnd.nextInt(256), rnd.nextInt(56), rnd.nextInt(256))
}
}
The general idea is to get GradientDrawable from your drawable then modify background, color, orientation and then apply to view.
Answered By - Kishan Maurya
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.