How to get the selected index of a RadioGroup in Android

In this android programming source code example, we are going to get the selected index of a RadioGroup in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to get the selected index of a RadioGroup in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:layout_margin="10dp"
    android:orientation="vertical"
    tools:context=".radiobutton.RadioButton12">

    <TextView
        android:id="@+id/textView25"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="RadioGroup Get Selected Index" />

    <RadioGroup
        android:id="@+id/radio"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:divider="?android:attr/dividerHorizontal"
        android:showDividers="middle">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton1" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton2" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton3" />


        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton4" />

    </RadioGroup>

    <Button
        android:text="Get Selected RadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/index"/>

</LinearLayout>

MainActivity.java

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 RadioButton12 extends AppCompatActivity {

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

        final RadioGroup radioGroup = findViewById(R.id.radio);
        Button getIndex = findViewById(R.id.index);

        getIndex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int selectedIndex = radioGroup.getCheckedRadioButtonId();
                // If nothing is selected from Radio Group, then it return -1
                if (selectedIndex != -1) {

                    RadioButton selectedRadioButton = findViewById(selectedIndex);
                    String selectedRadioButtonText = selectedRadioButton.getText().toString();

                    Toast.makeText(getApplicationContext(), selectedRadioButtonText + " selected", Toast.LENGTH_LONG).show();
                }
                else{
                    Toast.makeText(getApplicationContext(), "Nothing was selected", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment