AppCompatRadioButton

AppCompatRadioButton is a two states button which is either checked or unchecked. If a single radio button is unchecked, we can click it to make checked radio button. Once a radio button is checked, it cannot be marked as unchecked by user.

AppCompatRadioButton is generally used with RadioGroup. RadioGroup contains several radio buttons, marking one radio button as checked makes all other radio buttons as unchecked.

Class Hierarchy
public class RadioButton extends CompoundButton
java.lang.Object
↳ android.view.View
  ↳ android.widget.TextView
    ↳ android.widget.Button
      ↳ android.widget.CompoundButton
        ↳ android.widget.RadioButton

We Recommend to use new UI components i.e. androidx components

Note: AppCompatRadioButton is a widget used in Android for the grouping of AppCompatRadioButton's and provide the feature of selecting only one AppCompatRadioButton from the set. When a user try to select any other AppCompatRadioButton within same RadioGroup the previously selected radio button will be automatically unchecked.


Create AppCompatRadioButton in layout xml
<androidx.appcompat.widget.AppCompatRadioButton
        android:id="@+id/male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Male" />

Create AppCompatRadioButton in programatically
AppCompatRadioButton radioButton = new AppCompatRadioButton(this);
LinearLayoutCompat.LayoutParams params = new 
LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
radioButton.setLayoutParams(params);
radioButton.setText("Male");


We will discuss a program selecting gender below:

layout/radiobutton_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.appcompat.widget.AppCompatTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton Demo"
        android:textSize="12pt"
        android:layout_gravity="center_horizontal"
        android:textStyle="bold" />

    <RadioGroup
        android:id="@+id/rgGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="20dp"
        android:orientation="horizontal">

        <androidx.appcompat.widget.AppCompatRadioButton
            android:id="@+id/rbMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Male" />

        <androidx.appcompat.widget.AppCompatRadioButton
            android:id="@+id/rbFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female" />

    </RadioGroup>

    <androidx.appcompat.widget.AppCompatButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:onClick="show"
        android:text="Show Selected Gender" />
</androidx.appcompat.widget.LinearLayoutCompat>

RadioButtonActivity.java
package com.theitbulls.uicomponentex;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class RadioButtonActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.radiobutton_activity);
    }

    public void show(View view) {
        RadioGroup rgGroup = findViewById(R.id.rgGroup);
        if(rgGroup.getCheckedRadioButtonId() == R.id.rbMale) {
            Toast.makeText(this, "Gender selected is Male", Toast.LENGTH_SHORT).show();
        } else if(rgGroup.getCheckedRadioButtonId() == R.id.rbFemale){
            Toast.makeText(this, "Gender selected is Female", Toast.LENGTH_SHORT).show();
        }
    }
}