All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Radio Button In Android

Last Updated : Mar 11, 2024

Radio Button In Android

In this article we will show you the solution of radio button in android, a radio button is an element of the graphical user interface (GUI) in Android that enables users to choose one option from a list of mutually incompatible options.

The user's selection of a radio button automatically deselects all other items in the list. Due to this feature, radio buttons can be used to offer a list of options where only one can be chosen at a time.

The RadioButton class, a subclass of a Button class, is frequently used in Android to build radio buttons.

Although only one radio button within group can be selected at a time, they can be grouped into a single RadioGroup class.

The user can choose one choice from a group of options by using radio buttons.

If you feel that the customer needs to view all available alternatives side-by-side, utilize radio buttons with optional sets that really are mutually exclusive.

Use a spinner as a place of a list if it is not required to display all alternatives side by side.

In your layout, construct a RadioButton to represent each radio button option. But you must group radio buttons inside a RadioGroup because they are mutually exclusive.

In order to prevent more than one radio button from being selected at once, the system groups them together.

An on-click event is sent to the associated RadioButton object when the user clicks any of the radio buttons.

Add the android:onClick property to the <RadioButton > element inside your XML layout to specify the clicking event handler for just a button.

The name of a method you want to invoke in response to a click event must be the value of this attribute.

The matching method must subsequently be implemented by Activity hosting the layout. The method's signature must match exactly what is displayed above in the android:onClick attribute.

Step By Step Guide On Radio Button In Android :-

XML

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioButton android:id="@+id/radio_pirates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pirates"
        android:onClick="onRadioButtonClicked"/>
    <RadioButton android:id="@+id/radio_ninjas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ninjas"
        android:onClick="onRadioButtonClicked"/>
</RadioGroup>

Java

public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();
    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_pirates:
            if (checked)
            break;
        case R.id.radio_ninjas:
            if (checked)
            break;
    }
}
  1. Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
  2. Add the following code to res/layout/activity_main.xml.
  3. Add the following code to src/MainActivity.java
  4. Open res/values/strings.xml and add the following code
  5. Add the following code to androidManifest.xml

Conclusion :-

The radio button has only two states, which are checked or unchecked. We can click on a single radio button to make it checked if it is currently unchecked.

Once one radio button has been ticked, the user cannot mark it as unchecked.

RadioGroup is typically used with RadioButton. RadioGroup has multiple radio buttons; selecting one of them causes the other radio buttons to become unchecked.

I hope this article on radio button in android helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪