Here's an example using an extension. This assumes the view has the same width and height.
Need to use a layout change listener to get the view size.
Then you can just call this on a view like this myView.setRoundedBackground(Color.WHITE)
fun View.setRoundedBackground(@ColorInt color: Int) {
addOnLayoutChangeListener(object: View.OnLayoutChangeListener {
override fun onLayoutChange(v: View?, left: Int, top: Int, right: Int, bottom: Int, oldLeft: Int, oldTop: Int, oldRight: Int, oldBottom: Int) {
val shape = GradientDrawable()
shape.cornerRadius = measuredHeight / 2f
shape.setColor(color)
background = shape
removeOnLayoutChangeListener(this)
}
})
}