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.
Note: This Android article covered in both Java and Kotlin languages.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Step 2: Working with the activity_main.xml file
Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout android:id = "@+id/idRLContainer" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = ".MainActivity" > <!--displaying a simple text view--> < TextView android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_above = "@id/radioGroup" android:layout_margin = "15dp" android:text = "Radio Group in Android" android:textAlignment = "center" android:textColor = "@color/black" android:textSize = "20sp" android:textStyle = "bold" /> <!--displaying a radio group on below line--> < RadioGroup android:id = "@+id/radioGroup" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_centerInParent = "true" android:layout_gravity = "center" android:layout_marginStart = "10dp" android:layout_marginTop = "40dp" android:layout_marginEnd = "10dp" android:gravity = "center" > <!--adding a radio button --> < RadioButton android:id = "@+id/javaRB" android:layout_width = "200dp" android:layout_height = "wrap_content" android:layout_gravity = "center" android:checked = "false" android:padding = "4dp" android:text = "Java" android:textAlignment = "center" android:textSize = "20sp" /> <!--adding a radio button --> < RadioButton android:id = "@+id/cRB" android:layout_width = "200dp" android:layout_height = "wrap_content" android:layout_gravity = "center" android:checked = "false" android:padding = "4dp" android:text = "C++" android:textAlignment = "center" android:textSize = "20sp" /> <!--adding a radio button --> < RadioButton android:id = "@+id/pythonRB" android:layout_width = "200dp" android:layout_height = "wrap_content" android:layout_gravity = "center" android:checked = "false" android:padding = "4dp" android:text = "Python" android:textAlignment = "center" android:textSize = "20sp" /> </ RadioGroup > </ RelativeLayout > |
Step 3: Working with the MainActivity file
Navigate to app > java > your app’s package name > MainActivity file and add the below code to it. Comments are added in the code to get to know in detail.
Kotlin
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() } } } |
Java
package com.gtappdevelopers.googlemapsroutes; import android.os.Bundle; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { // on below line we are creating variables. private RadioGroup radioGroup; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // on below line we are initializing our variables. radioGroup = findViewById(R.id.idRVLanguages); // on below line we are adding check change listener for our radio group. radioGroup.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // on below line we are getting radio button from our group. RadioButton radioButton = findViewById(checkedId); // on below line we are displaying a toast message. Toast.makeText(MainActivity. this , "Selected Radio Button is : " + radioButton.getText(), Toast.LENGTH_SHORT).show(); } }); } } |
Now run your application to see the output of it.
Output: