I have to create a circle shape and paint it randomly but I have to do it programmatically. It has to be look like this
TextView that needs background
val tvAuthorSubtext = TextView(context)
tvAuthorSubtext.apply {
text = "AA"
textSize = 10f
setTextColor(ContextCompat.getColor(context, R.color.white))
gravity = Gravity.CENTER
typeface = ResourcesCompat.getFont(context, R.font.ubuntu)
layoutParams = LinearLayout.LayoutParams(72, 72)
setBackgroundResource(R.drawable.bg_author_shape)
}
R.drawable.bg_author_shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="12dp" />
<solid android:color="@color/black" />
</shape>
getBackgroundRandomColor Function
fun getRandomBackgroundColor() :Int {
val colorArray : MutableList<Int> = mutableListOf()
colorArray.add(R.color.aqua)
colorArray.add(R.color.bright_blue)
colorArray.add(R.color.bright_purple)
colorArray.add(R.color.bright_pink)
colorArray.add(R.color.bright_green)
colorArray.add(R.color.orangey_yellow)
colorArray.add(R.color.tealish)
val rnds = (0..(colorArray.size-1)).random()
return colorArray[rnds]
}
- Is there any way to run functions in drawable xmls?
- Is there any way to change shape drawable background color without changing its shape