Get checked radio button group value in android

How to retrieve selected radio button value.

Radio button is completely different from other tags because its gives single level choice selection to application user. Radio button is a part of radio group so radio button values can be easily accessible by radio groups. So here is the complete step by step tutorial for Get checked radio button group value in android.

android-project-download-code-button

Get checked radio button group value in android.

Code for MainActivity.java file.

package com.android_examples.com.getcheckedradiobuttondata;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity {

 RadioButton Male,Female;
 RadioGroup radioGroup;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 Male = (RadioButton)findViewById(R.id.radMale);
 Female = (RadioButton)findViewById(R.id.radFemale);
 radioGroup = (RadioGroup)findViewById(R.id.radGroup);
 
 radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
 
 @Override
 public void onCheckedChanged(RadioGroup group, int checkedId) {
 
 if(Male.isChecked())
 {
 Toast.makeText(MainActivity.this, "Male", Toast.LENGTH_SHORT).show();
 }
 else {
 Toast.makeText(MainActivity.this, "Female", Toast.LENGTH_SHORT).show();
 }
 
 
 }
 });
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.getcheckedradiobuttondata.MainActivity" >

 <RadioGroup
 android:id="@+id/radGroup"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="28dp"
 android:orientation="vertical" >

 <RadioButton
 android:id="@+id/radMale"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Male"
 android:typeface="serif" />

 <RadioButton
 android:id="@+id/radFemale"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Female"
 android:typeface="monospace" />
 </RadioGroup>

</RelativeLayout>

Screenshots:

How to get checked radio button group value in android

Click Here To Download Get checked radio button group value in android project.

Leave a Reply

Your email address will not be published. Required fields are marked *