RadioGroup in Android – GeeksforGeeks

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 

    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/idRLContainer"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context=".MainActivity">

  

    

    <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" />

  

    

    <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">

  

        

        <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" />

  

        

        <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" />

  

        

        <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() {

  

    

    lateinit var radioGroup: RadioGroup

  

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

         

        

        radioGroup = findViewById(R.id.radioGroup)

          

        

        

        radioGroup.setOnCheckedChangeListener { group, checkedId ->

              

            

            val radioButton = findViewById<RadioButton>(checkedId)

              

            

            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 {

  

    

    private RadioGroup radioGroup;

  

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

          

        

        radioGroup = findViewById(R.id.idRVLanguages);

          

        

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override

            public void onCheckedChanged(RadioGroup group, int checkedId) {

                  

                

                RadioButton radioButton = findViewById(checkedId);

                  

                

                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:

My Personal Notes

arrow_drop_up

Alternate Text Gọi ngay