Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaRadioButton in Android

RadioButton in Android

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. 

Note: This Android article covers 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: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">
  
    <!--on below line we are creating
        a text for our app-->
    <TextView
        android:id="@+id/idTVHeading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/idTVStatus"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:gravity="center"
        android:padding="10dp"
        android:text="Radio Button in Android"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold" />
  
    <!--on below line we are creating a text view-->
    <TextView
        android:id="@+id/idTVStatus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/idRadioGroup"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:gravity="center"
        android:padding="10dp"
        android:text="Status"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold" />
  
    <!--on below line we are creating a radio group-->
    <RadioGroup
        android:id="@+id/idRadioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center">
  
        <!--on below line we are creating a radio buttons-->
        <RadioButton
            android:id="@+id/idBtnJavaRadio"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text="Java"
            android:textAlignment="center"
            android:textColor="@color/black"
            android:textSize="20sp" />
  
        <RadioButton
            android:id="@+id/idBtnKotlinRadio"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text="Kotlin"
            android:textAlignment="center"
            android:textColor="@color/black"
            android:textSize="20sp" />
  
        <RadioButton
            android:id="@+id/idBtnFlutterRadio"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text="Flutter"
            android:textAlignment="center"
            android:textColor="@color/black"
            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 code below. Comments are added in the code to get to know in detail. 

Kotlin




package com.gtappdevelopers.kotlingfgproject
  
import android.os.Bundle
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
  
    // on below line we are creating a variable.
    lateinit var radioGrp: RadioGroup
    lateinit var statusTV: TextView
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // on below line we are initializing our variables.
        radioGrp = findViewById(R.id.idRadioGroup)
        statusTV = findViewById(R.id.idTVStatus)
  
        radioGrp.setOnCheckedChangeListener { group, checkedId ->
            // Get the selected Radio Button
            val radioButton = group
                .findViewById(checkedId) as RadioButton
  
            // on below line we are setting 
              // text for our status text view.
            statusTV.text = radioButton.text
        }
    }
}


Java




package com.gtappdevelopers.kotlingfgproject;
  
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    // on below line we are creating a variable.
    private RadioGroup radioGrp;
    private TextView statusTV;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // on below line we are initializing our variables.
        radioGrp = findViewById(R.id.idRadioGroup);
        statusTV = findViewById(R.id.idTVStatus);
  
        radioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // Get the selected Radio Button
                RadioButton radioButton = group.findViewById(checkedId);
  
                // on below line we are setting text 
                  // for our status text view.
                statusTV.setText(radioButton.getText());
            }
        });
    }
}


Now run your application to see the output of it. 

Output:

RELATED ARTICLES

Most Popular

Recent Comments