Radio Group/Button

Radio Group/Button

  • RadioGroup is a widget in android which is used to handle multiple radio buttons within the android application. We can add multiple radio buttons to our RadioGroup. We have seen how to use radio buttons in android. In this article, we will take a look at How to implement Radio Group in the android application. A sample video is given below to get an idea about what we are going to do in this article.
  • RadioButton is a widget used in android which provides functionality to choose a single option from multiple sets of options. This is generally used when we want the user to select only one option from the set of given options. In this article, we will take a look at How to use Radio Buttons on Android.
package com.gtappdevelopers.kotlingfgproject import android.os.Bundle import android.widget.* import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { // on below line we are creating variables. lateinit var radioGroup: RadioGroup override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // on below line we are initializing our variables. radioGroup = findViewById(R.id.radioGroup) // on below line we are adding check // change listener for our radio group. radioGroup.setOnCheckedChangeListener { group, checkedId -> // on below line we are getting radio button from our group. val radioButton = findViewById<RadioButton>(checkedId) // on below line we are displaying a toast message. Toast.makeText(this@MainActivity,"Selected Radio Button is : " + radioButton.text,Toast.LENGTH_SHORT ).show() } } }