In this tutorial we will learn about how to create radio button in android studio, and show a toast message of radio button value when user select it and radio button android example .

Radio Button

Android Radio buttons are used to show the multiple options, to select one option from it. Android radio button is allow the user to select one options from a group of options at a time.

Radio buttons allow the user to select one option from a set. You should use radio buttons for optional sets that are mutually exclusive . Radio button has two states, either checked or unchecked. Unlike checkbox, changes in the state of a radio button will affect other radio buttons. Radio button is an android widget, that belong to android.widget.RadioButton class.

Radio Group

RadioButtons android are used together in a radio group. RadioGroup is a group of radio buttons. Radio group is a widget of android.widget.RadioGroup class . Initially radio buttons are in unchecked state, Checking one radio button that belongs to a radio group unchecked any other checked radio button within the group . It means at one time only one radio button could be checked.

1. Create a New Project :

Lets we start with a new project in Android Studio from File New Project ⇒
select Empty Activity and go through it.

2. Create a RadioButton to your main layout file :

First we create a radio group then create the radio buttons inside the radio group to your main layout file i.e. activity_main.xml of your Main_Activity.java file or just implement the following xml code.

<RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp"
        tools:ignore="MissingConstraints">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android"
            android:textSize="15sp"/>

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Java"
            android:textSize="15sp"/>

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Php"
            android:textSize="15sp"/>

        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mysql"
            android:textSize="15sp"/>

    </RadioGroup>

activity_main.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=".TestActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Please select a course :-"
        android:textColor="@android:color/black"
        android:layout_marginRight="12dp"
        android:layout_marginLeft="13dp"
        android:layout_marginTop="4dp"
        android:textSize="21sp"/>

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp"
        tools:ignore="MissingConstraints">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android"
            android:textSize="15sp"/>

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Java"
            android:textSize="15sp"/>

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Php"
            android:textSize="15sp"/>



        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mysql"
            android:textSize="15sp"/>
    </RadioGroup>

</LinearLayout>

3. Implement OnCheckedChangeListener to radio group :

Now implement setOnCheckedChangeListener of radio group to perform an action or event on radio buttons, like toast message in MainActivity.java file or just add the following code .

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // checkedId is the Id of the RadioButton selected
                RadioButton radioButton=(RadioButton)findViewById(checkedId);
                Toast.makeText(getApplicationContext(), radioButton.getText(), Toast.LENGTH_SHORT).show();
            }
        });

MainActivity.java :

package net.technxt.radiobutton;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class TestActivity extends AppCompatActivity {

    RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        radioGroup = (RadioGroup)findViewById(R.id.radioGroup);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // checkedId is the Id of the RadioButton selected
                RadioButton radioButton=(RadioButton)findViewById(checkedId);
                Toast.makeText(getApplicationContext(), radioButton.getText(), Toast.LENGTH_SHORT).show();
            }
        });


    }
}

Now run the project/app to test radio button in android app through android device or any Emulator .

%d bloggers like this: