How to get the selected index of a RadioGroup in Android
You should be able to do something like this:
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);
If the RadioGroup
contains other Views (like a TextView
) then the indexOfChild()
method will return wrong index.
To get the selected RadioButton
text on the RadioGroup
:
RadioButton r = (RadioButton) radioButtonGroup.getChildAt(idx);
String selectedtext = r.getText().toString();
Set selected index of an Android RadioGroup
If your radio group is defined in a layout xml file, each button can be assigned an id. Then you just check a button like this
radioGroup.check(R.id.myButtonId);
If you created your radio group programmatically (I'm not even sure how you do this...), you might want to consider creating a special layout xml file just for the radio group so that you can assign R.id.* ids to the buttons.
Please see the answer below if you are, in fact, looking to set the radio button group by index, see the answer below.
((RadioButton)radioGroup.getChildAt(index)).setChecked(true);
Android getting value from selected radiobutton
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>
How to check which radio button of a radio group is selected? [ANDROID]
This is working perfectly:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
int radioButtonID = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) radioGroup.findViewById(radioButtonID);
String selectedText = (String) radioButton.getText();
Android RadioGroup get the RadioButton index
It is because you are adding radio button via code what getCheckedRadioButtonId does is look for radio buttons resource id, which can be done when you add Radio button in xml layout.
See this
<RadioGroup
android:id="@+id/radioGroupTeamId"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_male"
android:checked="true" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_female" />
</RadioGroup>
Or Check this post
You can manually keep track of the currently selected item or create a click listener on each radio button.
How to set selected index on RadioGroup programmatically?
Here is my current using codes, but not sure this is perfect one...
Get the index of a RadioGroup in Android
It crashes because pos is an integer change. If you pass an int value as second paramter you are asking android to look for a String with id the int you provide. If it does not exists the ResourcesNotFoundException
will be thrown
Toast.makeText(MainActivity.this, pos, Toast.LENGTH_SHORT).show();
with
Toast.makeText(MainActivity.this, String.valueOf(pos), Toast.LENGTH_SHORT).show();
Related Topics
Background Timer Task in Jsp/Servlet Web Application
How to Access a Folder Inside of a Resource Folder from Inside My Jar File
Javafx - What Is This Weird Container That Holds Context Menu
Customised Listview Using Arrayadapter Class in Android
Send Sms Until It Is Successful
Failed to Import New Gradle Project: Failed to Find Build Tools Revision *.0.0
What Is the Easiest Way to Get the Current Day of the Week in Android
Dynamically Get Drawables by Id
How to Reliably Simulate Touch Events on Android Without Root (Like Automate and Tasker)
Java.Sql.Sqlexception: No Suitable Driver Found for Jdbc:Microsoft:Sqlserver
Differencebetween ? and Object in Java Generics
How to Get the Selected Index of a Radiogroup in Android
Universal Way to Write to External Sd Card on Android
Java Doesn't Work with Regex \S, Says: Invalid Escape Sequence
Create Pdu for Android That Works with Smsmessage.Createfrompdu() (Gsm 3Gpp)