Android gets value from the selected radio

I have a piece of code with three RadioButton inside a RadioGroup . I want to install onCheckedListener , which will show the RadioButton value in Toas t, however, what I got so far does not work. How to get RadioButton value and display it in Toast ? Thanks in advance, this is my code:

 package com.example.toohelps; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.Toast; public class MainActivity extends Activity { RadioGroup rg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1); final String value = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())) .getText().toString(); rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { Toast.makeText(getBaseContext(), value, Toast.LENGTH_SHORT).show(); } }); } } 

XML 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=".MainActivity" > <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="152dp" > <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Choose 1" /> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Choose 2" /> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Choose 3" /> </RadioGroup> </RelativeLayout> 
+47
java android xml
Aug 12 '13 at 3:21
source share
10 answers

Tested and working. Check this

 import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class MyAndroidAppActivity extends Activity { private RadioGroup radioGroup; private RadioButton radioButton; private Button btnDisplay; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); addListenerOnButton(); } public void addListenerOnButton() { radioGroup = (RadioGroup) findViewById(R.id.radio); btnDisplay = (Button) findViewById(R.id.btnDisplay); btnDisplay.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // get selected radio button from radioGroup int selectedId = radioGroup.getCheckedRadioButtonId(); // find the radiobutton by returned id radioButton = (RadioButton) findViewById(selectedId); Toast.makeText(MyAndroidAppActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show(); } }); } } 

XML

 <RadioGroup android:id="@+id/radio" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/radioMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/radio_male" android:checked="true" /> <RadioButton android:id="@+id/radioFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/radio_female" /> </RadioGroup> 
+79
Aug 12 '13 at 3:30
source share

In case you want to do some work on choosing one of the switches (without any additional OK button or something else), your code is fine, updated a little.

 public class MainActivity extends Activity { RadioGroup rg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { switch(checkedId){ case R.id.radio0: // do operations specific to this selection break; case R.id.radio1: // do operations specific to this selection break; case R.id.radio2: // do operations specific to this selection break; } } }); } } 
+29
Jan 19 '14 at 4:21
source share
 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { radioButton = (RadioButton) findViewById(checkedId); Toast.makeText(getBaseContext(), radioButton.getText(), Toast.LENGTH_SHORT).show(); } } ); 
+7
May 21 '16 at 12:13
source share

just use the getCheckedRadioButtonId () function to determine if something will be checked, if -1 is a return, you can avoid a toast

+3
Jan 13 '16 at
source share

For those who programmatically program and look for an index, you may notice that checkedId changes when you return to activity / fragment, and you re-add these switches. One way around this is to set a tag with an index:

  for(int i = 0; i < myNames.length; i++) { rB = new RadioButton(getContext()); rB.setText(myNames[i]); rB.setTag(i); myRadioGroup.addView(rB,i); } 

Then in your listener:

  myRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radioButton = (RadioButton) group.findViewById(checkedId); int mySelectedIndex = (int) radioButton.getTag(); } }); 
+2
Feb 16 '17 at 22:07
source share
 int genid=gender.getCheckedRadioButtonId(); RadioButton radioButton = (RadioButton) findViewById(genid); String gender=radioButton.getText().toString(); 

Hope this works. You can convert your output to a string in the way described above. gender.getCheckedRadioButtonId(); - gender is the identifier of RadioGroup.

+1
Apr 18 '17 at 19:16
source share
 Radiogroup rgteam; String team; rgteam.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { RadioButton rb= (RadioButton) findViewById(checkedId); team = rb.getText().toString(); } }); 
+1
Jul 18 '17 at 4:49 on
source share

Thanks a lot to Chris.

This is my decision:

 RadioGroup radgroup_opcionesEventos = null; private static String[] arrayEventos = { "Congestiรณn", "Derrumbe", "Accidente" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); radgroup_opcionesEventos = (RadioGroup)findViewById(R.id.rg_opciones_evento); int i=0;//a.new.ln for(String evento : arrayEventos) { //RadioButton nuevoRadio = crearRadioButton(evento);//a.old.ln RadioButton nuevoRadio = crearRadioButton(evento,i);//a.new.ln radgroup_opcionesEventos.addView(nuevoRadio,i); } RadioButton primerRadio = (RadioButton) radgroup_opcionesEventos.getChildAt(0); primerRadio.setChecked(true); } private RadioButton crearRadioButton(String evento, int n) { //RadioButton nuevoRadio = new RadioButton(this);//a.old.ln RadioButton nuevoRadio = new RadioButton(getApplicationContext());//a.new.ln LinearLayout.LayoutParams params = new RadioGroup.LayoutParams( RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT); nuevoRadio.setLayoutParams(params); nuevoRadio.setText(evento); nuevoRadio.setTag(evento); return nuevoRadio; } @Override protected void onResume() { radgroup_opcionesEventos.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radioButton = (RadioButton) group.findViewById(checkedId); //int mySelectedIndex = (int) radioButton.getTag(); String mySelectedIndex = radioButton.getTag().toString(); } }); super.onResume(); } 
0
May 08 '17 at 2:00 a.m.
source share
 mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { RadioButton radioButton = (RadioButton)group.findViewById(checkedId); } }); 
0
Oct 27 '17 at 6:17
source share

I had problems getting the ID of the radio books, as well as the dynamic creation of RadioButtons. This does not seem to work if you are trying to manually set the identifier using RadioButton.setId() . For me, it was to use View.getChildAt() and View.getParent() to View.getParent() over the radio buttons and determine which one was checked. All you need to do is get RadioGroup first through findViewById(R.id.myRadioGroup) and then findViewById(R.id.myRadioGroup) over the children through it. You will learn how you iterate through which button you are on, and you can simply use RadioButton.isChecked() to determine if that button has been checked.

-one
Dec 17 '15 at 17:10
source share



All Articles