activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF5722"
android:textSize="30sp"
android:textStyle="bold"
android:textAllCaps="true"
android:padding="10dp"
android:layout_margin="10dp"
android:gravity="center"
android:layout_gravity="center"
android:id="@+id/tv1"
/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/rg">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/r1"
android:text="Male"></RadioButton>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/r2"
android:text="Female"></RadioButton>
</RadioGroup>
</LinearLayout>
MainActivity.kt
package com.androinidan.checkboxkotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.Switch
import android.widget.TextView
import android.widget.ToggleButton
class MainActivity : AppCompatActivity() {
var radiogroup: RadioGroup?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tv = findViewById(R.id.tv1)
radiogroup=findViewById(R.id.rg)
r1=findViewById(R.id.r1)
radiogroup.setOnCheckedChangeListener{ group, checkedId ->
if(checkedId==R.id.r1)
tv!!.text=("MALE")
else
tv!!.text=("FEMALE")
}
}
}