Tuesday, July 28, 2020

android kotlin - RadioGroup onCheckedChange listener

MainActivity.kt

package com.example.jetpack

import android.os.Bundle
import android.widget.RadioButton
import androidx.appcompat.app.AppCompatActivity
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
            }
        }
    }
}
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="#F2E8D7">

    <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/radioDreamweaver"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Dreamweaver" />

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

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

        <com.google.android.material.radiobutton.MaterialRadioButton
            android:id="@+id/radioPhotoshop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Photoshop" />

    </RadioGroup>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:textColor="#F400A1"
        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>