Android UI How to - RadioGroup Checked Change Listener








The following code shows how to listen to selection changed event on a RadioGroup.

Example

Layout xml

/*from w  ww .  j av a  2  s  .  c  o  m*/
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <RadioButton
        android:id="@+id/anotherRadBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Outside" />

    <RadioGroup
        android:id="@+id/radGrp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/chRBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Chicken" />

        <RadioButton
            android:id="@+id/fishRBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fish" />

        <RadioButton
            android:id="@+id/stkRBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Steak" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="My Favorite" />

    </RadioGroup>
</LinearLayout>

Java code

package com.java2s.app;
//w w w .ja  v  a 2 s. c  o  m
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioGroup;

public class MainActivity extends Activity {

    protected static final String TAG = "RadioGroupActivity";

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

        RadioGroup radGrp = (RadioGroup) findViewById(R.id.radGrp);

        int checkedRadioButtonId = radGrp.getCheckedRadioButtonId();

        radGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup arg0, int id) {
                switch (id) {
                    case -1:
                        Log.v(TAG, "Choices cleared!");
                        break;
                    case R.id.chRBtn:
                        Log.v(TAG, "Chose Chicken");
                        break;
                    case R.id.fishRBtn:
                        Log.v(TAG, "Chose Fish");
                        break;
                    case R.id.stkRBtn:
                        Log.v(TAG, "Chose Steak");
                        break;
                    default:
                        Log.v(TAG, "Huh?");
                        break;
                }
            }
        });
    }

}
null

We can get the currently checked RadioButton using getCheckedRadioButtonId(), which returns the resource ID of the checked item or -1 if nothing is checked.