Tuesday, July 28, 2020

android kotlin - Change checked RadioButton text color

MainActivity.kt

package com.example.jetpack

import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.widget.RadioButton
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.forEach
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

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

        // 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
                textView2.text = text

                // update radio button style based on checked status
                updateRadioButtonStyle()
            }
        }
    }


   // function to change checked radio button color
   private fun updateRadioButtonStyle(){
        radioGroup.forEach {
            (it as RadioButton).apply {
                if(isChecked){
                    setTextColor(Color.parseColor("#E63E62"))
                    setBackgroundColor(Color.parseColor("#FFECEBEA"))
                    setTypeface(null, Typeface.BOLD_ITALIC)
                }else{
                    setTextColor(Color.DKGRAY)
                    setBackgroundColor(Color.TRANSPARENT)
                    setTypeface(null, Typeface.NORMAL)
                }
            }
        }
    }
}
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">

    <TextView
        android:id="@+id/textView"
        style="@style/TextAppearance.MaterialComponents.Headline6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:padding="8dp"
        android:text="Which is your most favorite?"
        android:typeface="monospace"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="2dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView">

        <com.google.android.material.radiobutton.MaterialRadioButton
            android:id="@+id/radioFireworks"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingEnd="50dp"
            android:text="Fireworks" />

        <com.google.android.material.radiobutton.MaterialRadioButton
            android:id="@+id/radioColdFusion"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingEnd="50dp"
            android:text="ColdFusion" />

        <com.google.android.material.radiobutton.MaterialRadioButton
            android:id="@+id/radioFlash"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingEnd="50dp"
            android:text="Flash" />

    </RadioGroup>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:textColor="#9678B6"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioGroup"
        tools:text="TextView" />

</androidx.constraintlayout.widget.ConstraintLayout>