RadioButton & RadioGroup Tutorial With Example In Android Studio

RadioButton : RadioButon is a two state button that can be checked or unchecked. If a radio button is unchecked then a user can check it by simply clicking on it. Once a RadioButton is checked by a user it can’t be unchecked by simply pressing the same button. It will automatically unchecked when you press any other RadioButton within same RadioGroup.

Step 1: Open up Android Studio Project through res -> layout -> activity_main.xml (or) main.xml and add below code or just drag and drop the switch to design XML file.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#e0e0e0"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Select your favourite wwe Fruit :: "
            android:textColor="#000"
            android:textSize="20sp"
            android:textStyle="bold" />

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RadioButton
                android:id="@+id/apple"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:checked="true"
                android:text="@string/apple"
                android:textColor="#154"
                android:textSize="20sp"
                android:textStyle="bold" />

            <RadioButton
                android:id="@+id/mango"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:checked="false"
                android:text="@string/mango"
                android:textColor="#154"
                android:textSize="20sp"
                android:textStyle="bold" />

           

        </RadioGroup>

        <Button
            android:id="@+id/submitButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="20dp"
            android:background="#0f0"
            android:padding="10dp"
            android:text="Submit"
            android:textColor="#fff"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>


</LinearLayout>

Step 2:Add below code in MainActivity.java to handle events on switch.

package com.bytelogs.example;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    RadioButton apple,mango;
    String fruit;
    Button submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        apple = (RadioButton) findViewById(R.id.apple);
        mango = (RadioButton) findViewById(R.id.randyOrton);
        submit = (Button) findViewById(R.id.submitButton);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (apple.isChecked()) {
                    fruit = apple.getText().toString();
                } else(mango.isChecked()) {
                    fruit = apple.getText().toString();
                } 
            
                Toast.makeText(getApplicationContext(), fruit, Toast.LENGTH_LONG).show(); // print the value of selected super star
            }
        });
    }
}

Step 3: Open res -> values -> strings.xml.

<resources>
    <string name="app_name">RadioButtonExample</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="apple">Apple</string>
    <string name="mango">Mango</string>
</resources>