Fri. Apr 26th, 2024

In Android, you can use “android.widget.RadioButton” class to render radio button, and those radio buttons are usually grouped by android.widget.RadioGroup. If RadioButtons are in group, when one RadioButton within a group is selected, all others are automatically deselected.
we show you how to use XML to create two radio buttons, and grouped in a radio group. When button is clicked, display which radio button is selected.

activitymain.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:id="@+id/tv"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:textColor="#FF5722"
        android:textSize="30sp"
        android:textAllCaps="true"
        android:textAlignment="center"/>

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rg"
        android:orientation="horizontal">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Male"
            android:layout_margin="5dp"
            android:padding="5dp"
            android:textColor="#FF5722"
            android:textSize="30sp"
            android:textAllCaps="true"
            android:textAlignment="center"
            android:id="@+id/r1"></RadioButton>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female"
            android:layout_margin="5dp"
            android:padding="5dp"
            android:textColor="#FF5722"
            android:textSize="30sp"
            android:textAllCaps="true"
            android:textAlignment="center"
            android:id="@+id/r2"></RadioButton>

    </RadioGroup>

    

   

</LinearLayout>

MainActivity.Java

package com.androinidan.radiobuttonexample;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    RadioGroup radioGroup;
    TextView textView;

    ToggleButton toggleButton;

    Switch swich1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radioGroup=findViewById(R.id.rg);
        textView=findViewById(R.id.tv);

        toggleButton=findViewById(R.id.toggleButton);
        swich1=findViewById(R.id.switch1);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if(i==R.id.r1){
                    textView.setText("Male");

                }else{
                    textView.setText("Female");
                }

            }
        });
   
    }
}

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *