Radio buttons in kotlin allow the user to select one option at a time. A RadioButton
as two states, selected and unselected.
Create a new Project in Kotlin
- Open Android Studio.
- Go to File => New => New Project. Write application name . Then, check Include Kotlin Support and click next button.
- Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
- Then, select Empty Activity => click next => click finish.
- You will get a newly created project successfully if you have followed steps properly.
Use the below code to Implement a Radio Buttons with Kotlin
MainActivity.kt
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*
import kotlinx.android.synthetic.main.activity_main.*
import android.widget.RadioGroup
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Radio button on click change
radio_group.setOnCheckedChangeListener(
RadioGroup.OnCheckedChangeListener{
group, checkedId ->
val radio_langange: RadioButton = findViewById(checkedId)
Toast.makeText(applicationContext," On Checked change :${radio_langange.text}",Toast.LENGTH_SHORT).show()
}
)
// Get radio group selected status and text using button click event
button.setOnClickListener{
// Get the checked radio button id from radio group
var id: Int = radio_group.checkedRadioButtonId
if (id!=-1){ // If any radio button checked from radio group
// Get the instance of radio button using id
val radio:RadioButton = findViewById(id)
Toast.makeText(applicationContext,"On button click : ${radio.text}",
Toast.LENGTH_SHORT).show()
}else{
// If no radio button checked in this radio group
Toast.makeText(applicationContext,"On button click : nothing selected",
Toast.LENGTH_SHORT).show()
}
}
}
// Get the selected radio button text using radio button on click listener
fun radio_button_click(view: View){
// Get the clicked radio button instance
val radio: RadioButton = findViewById(radio_group.checkedRadioButtonId)
Toast.makeText(applicationContext,"On click : ${radio.text}",
Toast.LENGTH_SHORT).show()
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
>
<RadioGroup
android:layout_marginTop="30dp"
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#bccdd5"
android:padding="15dp"
>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Which is your favorite languages?"
android:textStyle="bold"
android:textSize="20sp"
/>
<RadioButton
android:id="@+id/tvenglish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="English"
android:onClick="radio_button_click"
/>
<RadioButton
android:id="@+id/tvspanish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Spanish"
android:onClick="radio_button_click"
/>
<RadioButton
android:id="@+id/tvfrench"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="French"
android:onClick="radio_button_click"
/>
<RadioButton
android:id="@+id/tvmalayalam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Malayalam"
android:onClick="radio_button_click"
/>
</RadioGroup>
<Button
android:id="@+id/button"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Get Selected Language"
/>
</LinearLayout>
Output