techStackGuru

Radio Button in Android


Basically, there are two states of a Radio Buttonchecked and unchecked. When a user clicks on an unchecked Radio Button, it obtains the checked state, making the previously checked Radio Button (if there is any) unchecked.

In an Android Radio Button widget, each option refers to a radio button. Also, all the presented options in this tool are collectively referred to as a Radio Group. In fact, there is a separate widget of Radio Group as well, i.e. android.widget.RadioGroup. To clarify it further, Radio Buttons are used inside a Radio Group, and those Radio Buttons are grouped together by android.widget.RadioGroup.


Example of Radio Button

MainActivity.java


package com.techstackguru.radio;

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

public class MainActivity extends AppCompatActivity {

    private RadioGroup genderGroup;
    private RadioButton genderButton;
    private Button showSelected;

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

        genderGroup = (RadioGroup) findViewById(R.id.radioGender);
        showSelected = (Button) findViewById(R.id.button);

        showSelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int getId = genderGroup.getCheckedRadioButtonId();

                genderButton = (RadioButton) findViewById(getId);

                Toast.makeText(MainActivity.this,
                        genderButton.getText(), Toast.LENGTH_SHORT).show();
            }
        });

    }
}

After coding in the MainActivity file, open the second file of of your project. This is the strings.xml file where you will code to add some custom strings in your project, such as “Male” and “Female.” Now, enter the following code in the file:

strings.xml


<resources>
    <string name="app_name">Radio</string>
    <string name="male">Male</string>
    <string name="female">Female</string>
    <string name="display">Show Selected</string>
</resources>

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/radioGender"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp">

        <RadioButton
            android:id="@+id/radioMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:p="@string/male"
            android:textStyle="bold"
            android:textSize="22sp"
            android:checked="true" />

        <RadioButton
            android:id="@+id/radioFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:p="@string/female"
            android:layout_marginTop="10dp"
            android:textStyle="bold"
            android:textSize="22sp" />

    </RadioGroup>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:p="@string/display" />

</LinearLayout>

Once you are done with entering all the above-mentioned codes in the right files, you will be able to launch your project. After launching your project, your output would look like the following image:

Output
radio-button-1

previous-button
next-button