android studio background color code example

Example 1: android studio set background color

YourView.setBackgroundColor(Color.argb(255, 255, 255, 255));

Example 2: how to set background to general view in android

check if the view is Viewgroup or not
fun View.customBg(backgroundColor: Int, borderColor: Int) {

    val shape = GradientDrawable()
    shape.shape = GradientDrawable.RECTANGLE
    shape.cornerRadii = floatArrayOf(8f, 8f, 8f, 8f, 0f, 0f, 0f, 0f)
    shape.setColor(backgroundColor)
    shape.setStroke(3, borderColor)


    if (this is ViewGroup) {
        this.background = shape
    }

}

Tags:

Misc Example