What is Radio Group in android?

Before getting into example, we should know, What is radio group in android. Radio Group contains group of radio buttons. Using radio buttons we can select and un select according to user requirements.

This example demonstrate about how to use radio group in android.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:text="Select your favourite subject"
      app:layout_constraintTop_toTopOf="parent" />
         android:text="C Programming"/>
      android:layout_height="wrap_content" />

In the above code it contained four radio buttons, When user click on the button it going to get the text of checked radio button.

Step 3 − Add the following code to src/MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.util.Linkify;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
   RadioButton radioButton;
   protected void onCreate(Bundle savedInstanceState) {
      final Button button=findViewById(R.id.buton);
      final RadioGroup radioGroup=findViewById(R.id.radioGroup);
      button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            int clickedRadioButton=radioGroup.getCheckedRadioButtonId();
            if(clickedRadioButton ==-1){
            } else {

Step 5 − No need to change manifest.xml Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Select any subject, as an example we have selected java as shown below -

Now Click on ok button to get the text of selected radio button from radio group.

In the above result we have selected ok button, it will show toast of selected radio button text.

Click here to download the project code