How to Get the Selected Index of a Radiogroup in Android

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...

Sample Image

Sample Image

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



Leave a reply



Submit