Saturday, August 8, 2015

How to get selected item from a RadioGroup in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#fff6e4"
    >
    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/holo_red_dark"
        android:paddingBottom="15dp"
        />
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Which is your most favorite?"
        android:layout_below="@+id/tv_result"
        />
    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@id/tv"
        android:padding="15dp"
        >
        <RadioButton
            android:id="@+id/rb_coldfusion"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ColdFusion"
            android:paddingRight="15dp"
            />
        <RadioButton
            android:id="@+id/rb_flex"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Flex"
            android:paddingRight="15dp"
            />
    </RadioGroup>
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get Selected Radio Button"
        android:layout_below="@+id/rg"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends Activity {

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

        Button btn = (Button) findViewById(R.id.btn);
        final TextView tv_result = (TextView) findViewById(R.id.tv_result);

        final RadioGroup rg = (RadioGroup) findViewById(R.id.rg);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Get the checked Radio Button ID from Radio Grou[
                int selectedRadioButtonID = rg.getCheckedRadioButtonId();

                // If nothing is selected from Radio Group, then it return -1
                if (selectedRadioButtonID != -1) {

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

                    tv_result.setText(selectedRadioButtonText + " selected.");
                }
                else{
                    tv_result.setText("Nothing selected from Radio Group.");
                }
            }
        });
   }
}
More android examples