Wednesday, July 29, 2020

android kotlin - RadioButton circle color programmatically

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.content.res.ColorStateList
import android.graphics.BlendMode
import android.graphics.Color
import android.graphics.PorterDuff
import android.os.Build
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Get the widgets reference from XML layout
        val radioGroup = findViewById<RadioGroup>(R.id.radioGroup)
        val radioAndroid = findViewById<RadioButton>(R.id.radioAndroid)
        val radioKotlin = findViewById<RadioButton>(R.id.radioKotlin)
        val radioFireworks = findViewById<RadioButton>(R.id.radioFireworks)
        val radioColdFusion = findViewById<RadioButton>(R.id.radioColdFusion)
        val tvResult = findViewById<TextView>(R.id.tvResult)

        // change the radio buttons button color
        radioAndroid.setCircleColor(Color.parseColor("#1974D2"))
        radioKotlin.setCircleColor(Color.parseColor("#C32148"))
        radioFireworks.setCircleColor(Color.parseColor("#696969"))
        radioColdFusion.setCircleColor(Color.parseColor("#FFBA00"))

        // radio group checked change listener
        radioGroup.setOnCheckedChangeListener { radioGroup, i ->
            // get the radio group checked radio button
            findViewById<RadioButton>(i)?.apply {
                // show the checked radio button's text in text view
                tvResult.text = text
            }
        }
    }
}


// extension function to change radio button circle color programmatically
fun RadioButton.setCircleColor(color: Int){
    val colorStateList = ColorStateList(
        arrayOf(
            intArrayOf(-android.R.attr.state_checked), // unchecked
            intArrayOf(android.R.attr.state_checked) // checked
        ), intArrayOf(
            Color.GRAY, // unchecked color
            color // checked color
        )
    )

    // finally, set the radio button's button tint list
    buttonTintList = colorStateList

    // optionally set the button tint mode or tint blend mode
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        buttonTintBlendMode = BlendMode.SRC_IN
    }else{
        buttonTintMode = PorterDuff.Mode.SRC_IN
    }

    invalidate() //could not be necessary
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#E5E4E2"
    android:padding="24dp">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:padding="8dp"
        android:text="Which is your most favorite?"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvTitle">

        <RadioButton
            android:id="@+id/radioAndroid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android" />

        <RadioButton
            android:id="@+id/radioKotlin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Kotlin" />

        <RadioButton
            android:id="@+id/radioFireworks"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fireworks" />

        <RadioButton
            android:id="@+id/radioColdFusion"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ColdFusion" />

        <RadioButton
            android:id="@+id/radioFlash"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Flash" />
    </RadioGroup>

    <TextView
        android:id="@+id/tvResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:textColor="#32174D"
        android:textSize="30sp"
        android:fontFamily="sans-serif"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioGroup" />

</androidx.constraintlayout.widget.ConstraintLayout>