Multiple RadioGroup onCheckedChanged - android

This is my first time doing radio groups. Im trying to save the values from 3 groups to Shared Prefs. It works fine with one group, but once I try to add 2 more, all the pref values are just the last group in the form. Im not sure what to do to use multiple RadioGroups. Any help would be appreciated.
onCreate
RadioGroup radioGroupGender = (RadioGroup) findViewById(R.id.gender_radio);
radioGroupGender.setOnCheckedChangeListener(this);
RadioGroup radioGroupActivity = (RadioGroup) findViewById(R.id.activity_level);
radioGroupActivity.setOnCheckedChangeListener(this);
RadioGroup radioGroupStatus = (RadioGroup) findViewById(R.id.current_status);
radioGroupStatus.setOnCheckedChangeListener(this);
And my onCheckedChanged
public void onCheckedChanged(RadioGroup radioGroup, int i) {
int genderId = radioGroup.getCheckedRadioButtonId();
RadioButton genderBut = (RadioButton)radioGroup.findViewById(genderId);
gender_radio = genderBut.getText().toString();
int activityLvl = radioGroup.getCheckedRadioButtonId();
RadioButton activityLvlBut = (RadioButton)radioGroup.findViewById(activityLvl);
activity_level = activityLvlBut.getText().toString();
int currentStat = radioGroup.getCheckedRadioButtonId();
RadioButton CurrentBut = (RadioButton)radioGroup.findViewById(currentStat);
current_status = CurrentBut.getText().toString();
}

You are using the same gender_radio activity_level current_status , they get overwriten on every onCheckedChanged() call, only the values of the last call of onCheckedChanged() will be saved

Related

Radiogroup checked id getting wrong value -1

I want to access the RadioButton from the RadioGroup,
int selectedId = radioFeeGroup.getCheckedRadioButtonId();
radioFeeButton = (RadioButton) findViewById(selectedId);
I am getting selectedId value -1. That's why application get crashed.
Make Sure you are selecting at least one Radio button from the group.
See the Document
Your code is correct,
int selectedId = radioFeeGroup.getCheckedRadioButtonId();
radioFeeButton = (RadioButton) findViewById(selectedId);
If you are already selecting the RadioButton then clean the project & Rebuild it again it should work if you are accessing RadioGroup correctly from xml.
Check this
radioFeeGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int radioButtonID = radioFeeGroup.getCheckedRadioButtonId();
View radioButton = radioFeeGroup.findViewById(radioButtonID);
int position = group.indexOfChild(radioButton);
}
});
Check this examples
How to get the id of selected radio button in android?
How to get id of selected radio button in a radio group

Android -get radio button id based on value

I want to highlight right and wrong option when user clicks a button after checking an radio button. If that option is right, highlight. Else highlight right option.
Is there any way to get radio button id in a group based on its value? Or Do I need to use switch case?
I searched enough but not able to find out what I need.
Edit
I have simple layout which contains one question, 4 choices, one button. User check a radio button and click the check button. If user selects wrong option, highlight the correct option. I know what is correct option by value.
choice1, choice2, choice3, choice4 are four radio buttons. User checks choice3. But choice2 is correct. How do I select choice2 by value in this radio group.
group.getRadioButtonId("choice2")
Anything similar to this?
You could iterate through the children of your RadioGroup to get the required one:
int count = radioGroup.getChildCount();
for (int i = 0 ; i < count; i++) {
RadioButton button = (RadioButton) radioGroup.getChildAt(i);
if (button.getText().equals("choice2")) {
int id = button.getId(); // the ID you're looking for
}
}
You Can check like this
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
int rbId = group.getCheckedRadioButtonId();
RadioButton rb = (RadioButton) findViewById(rbId);
switch (group.getId()) {
case R.id.radiogroupID:
if (rb.getText().toString().equalsIgnoreCase("right")) {
// your logic
} else {
//your logic
}
}
}
you can use these line. its working for me
int checkedId = radioGroup.getCheckedRadioButtonId();
View radioButton = radioGroup.findViewById(checkedId);
int radioId = radioGroup.indexOfChild(radioButton);
RadioButton btn = (RadioButton) radioGroup.getChildAt(radioId);
First check which option is true. hear choice 2 OK. set by default is checked true and than check user checked value . if user select other option than display message and pass true option we already checked true
Create an int array with all the ids of the radiobutton
int[] rb_IDs=new int[]{R.id.rb1,R.id.rb2,R.id.rb3};
Then inside your button click :
RadioGroup rg=(RadioGroup) findViewById(R.id.rg);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for (int i=0;i<rb_IDs.length;i++)
{
RadioButton rb=(RadioButton)findViewById(rb_IDs[i]);
if (rb.getText().toString().equals("YOUR CORRECT ANSWER")&&!rb.isChecked())
{
//
//Do your thing
//
//ID of the correct answer Radiobutton is
int id=rb_IDs[i];
//HighLighting the answer
rb.setBackgroundColor(Color.parseColor("#00ff00"));
break;
}
}
}
});

Retrieve Value of RadioButton checked

Since several days, I search how to retrieve the checked radio Button ID, and after display the RadioButton checked in a Toast. But I get NULLPOINTER EXECEPTION for the int ID_LANGUE. I use this code :
enter code here
int ID_LANGUE = radioGroup_LANGUE.getCheckedRadioButtonId();
RadioButton rb_L = (RadioButton)findViewById(ID_LANGUE);
if (rb_L.equals("Anglais")){
Toast.makeText(MainActivity.this,"Anglais",Toast.LENGTH_LONG).show();
}
You have to get the radio group first by
RadioGroup rg = (RadioGroup) findViewById(R.id.RadioGroup_LANGUE);
Then you can use setOnCheckedChangeListener to determine which radio button was clicked actually.
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.radio0:
//you selected radio0
break;
case R.id.radio1:
//you selected radio1
break;
}
}
});
Other way is using getCheckedRadioButtonId also int id = radioGroup.getCheckedRadioButtonId(); you get hold of selected radio id.

How to get all of unchecked radio buttons in android

I have 16 Radio Group in my layout and i have 40 Radio Button . I want to get which Radio Button is unchecked in Radio Groups. I want to know how can i know is there any unchecked Radio Button in my layout thanks
You should probably group all of your buttons like so:
RadioGroup rg = (RadioGroup) findViewById(R.id.my_radio_group);
List<RadioButton> radioButtonsList = new ArrayList<>();
for(int i = 0; i < rg.getChildCount(); ++i) {
RadioButton b = rg.getChildAt(i);
if(b.isChecked()) radioButtonsList.add(b);
}
Do it for all of your groups and you'll have all your unchecked buttons in a list.
Also you can use:
int checkedRadioButtonId = rg.getCheckedRadioButtonId()
to get only checked button's id.
ArrayList<RadioGroup> radioGroupList = new ArrayList<RadioGroup>();
RadioGroup group1 = (RadioGroup)findViewById(...);
RadioGroup group2 = (RadioGroup)findViewById(...);
.
.
RadioGroup group16 = (RadioGroup)findViewById(...);
radioGroupList.add(group1);
radioGroupList.add(group2);
.
.
radioGroupList.add(group16);
and later you can check which is checked or not with this
for(RadioGroup radioButtonGroup:RadioGroupList){
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);
}
or if it's the RadioButtons you are interested in then add them in an ArrayList the same way and loop in that list like this
for(RadioButton radioButton:radioButtonList){
boolean isChecked = radioButton.isChecked();
}
You can check the state of radio buttons by using the isChecked() method.
This question has already been answered here:
How to check if "Radiobutton" is checked?

How to add a listener to dynamically created RadioButtons?

I have a ListView of questions. In each row there is a question and a RadioGroup with possibile answers. Not all the questions have the same number of answers, so I add the RadioButtons dynamically in my custom ListAdapter. My question is how to add Listener to these dynamically created RadioButtons. My code:
RadioGroup rg = (RadioGroup) convertView.findViewById(R.id.question);
rg.removeAllViews();
for (String qA : question.answers) {
RadioButton rb = new RadioButton(getContext());
rb.setText(qA.toString());
rg.addView(rb);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId) {
//WHAT SHOULD I PUT HERE?
}
}
});
Maby something like this:
#Override public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton btn = (RadioButton)findViewById(checkedId);
}
and then something with the btn? :)
To find the checked button
View checkedButton = rg.findViewById(checkedId);
To find the index of checked button
int checkedIndex = rg.indexOfChild(checkedButton);
Before doing the above you have to set id to the buttons like mentioned in the comments rb.setId(count). Count has to be unique for each button within a group.
1 first create global variable of CompoundButton.OnCheckedChangeListener in your adapter for example private CompoundButton.OnCheckedChangeListener listener
2 second you have to attach listener variable with your radio button at the time of creating the radio button.
3 third when you bind the data or value with your ui you have to call this method
listener = new CompoundButton.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged ( CompoundButton buttonView, boolean isChecked )
{
}
};

Categories

Resources