How To Get The Text From Radio Button In A Radio Group When Radio Button Checked State Is Changed
I have created two radio buttons in a radio group dynamically and one of them is checked. I need when i cheked another button then its value should be saved in string. But i have i
Solution 1:
xml file
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><RadioGroupandroid:id="@+id/radioSex"android:layout_width="wrap_content"android:layout_height="wrap_content" ><RadioButtonandroid:id="@+id/radioMale"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/radio_male"android:checked="true" /><RadioButtonandroid:id="@+id/radioFemale"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/radio_female" /></RadioGroup><Buttonandroid:id="@+id/btnDisplay"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/btn_display" /></LinearLayout>
java code
package com.mkyong.android;
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;
publicclassMyAndroidAppActivityextendsActivity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
publicvoidaddListenerOnButton() {
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
// get selected radio button from radioGroupintselectedId= radioSexGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioSexButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
radioSexButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
Solution 2:
try following code
radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
publicvoidonCheckedChanged(RadioGroup group, int checkedId) {
int id=group.getCheckedRadioButtonId();
RadioButton rb=(RadioButton) findViewById(id);
OR
RadioButton rb=(RadioButton) findViewById(checkedId);
String radioText=rb.getText().toString();
}
});
Solution 3:
try this...
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
publicvoidonCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
String text = radioButton.getText().toString();
updated_list.add(text);
}
});
Solution 4:
Kotlin code for retrieving text from a checked RadioButton in RadioGroup
binding.genderRadiogroup.setOnCheckedChangeListener { _, _ ->
var id: Int = binding.genderRadiogroup.checkedRadioButtonId
var rb: RadioButton = binding.genderRadiogroup.findViewById(id)
Toast.makeText(this,rb.text.toString(),Toast.LENGTH_SHORT).show()
}
Solution 5:
Try this solution for retrieving the text:
radiogroup.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener() {
voidonCheckedChanged(RadioGroup rg, int checkedId) {
for (inti=0; i < rg.getChildCount(); i++) {
RadioButtonbtn= (RadioButton) rg.getChildAt(i);
if (btn.getId() == checkedId) {
Stringtext= btn.getText();
// do something with textreturn;
}
}
}
});
Post a Comment for "How To Get The Text From Radio Button In A Radio Group When Radio Button Checked State Is Changed"