Reputation: 1882
I am developing an quiz based app. There will be 1 question and 4 option (radio buttons) when user opens this app radio button will be unchecked but the problem comes when the user answers 1 quest and when he goes for next quest radio button will be checked. I want to uncheck/reset the radio buttons for every question. How can I do it?
answ1=new ArrayList<String>(new ArrayList<String>(answ1));
btn_practice1.setText(answ1.get(0));
btn_practice2.setText(answ1.get(1));
btn_practice3.setText(answ1.get(2));
btn_practice4.setText(answ1.get(3));
btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = (RadioButton)group. findViewById(checkedId); //line 262
String temp = radioButton.getText().toString();
switch(btn_practicerg.getCheckedRadioButtonId()){
case R.id.RB1:
if (btn_practice1.isChecked()){
btn_practice2.setChecked(false);
btn_practice3.setChecked(false);
btn_practice4.setChecked(false);
}
break;
case R.id.RB2:
if (btn_practice2.isChecked()){
btn_practice1.setChecked(false);
btn_practice3.setChecked(false);
btn_practice4.setChecked(false);
}
break;
case R.id.RB3:
if (btn_practice3.isChecked()){
btn_practice1.setChecked(false);
btn_practice2.setChecked(false);
btn_practice4.setChecked(false);
}
break;
case R.id.RB4:
if (btn_practice4.isChecked()){
btn_practice1.setChecked(false);
btn_practice2.setChecked(false);
btn_practice3.setChecked(false);
}
break;
default:
btn_practice1.setChecked(false);
btn_practice2.setChecked(false);
btn_practice3.setChecked(false);
btn_practice4.setChecked(false);
}
ImageView nextBtn = (ImageView) findViewById(R.id.nxt_btn);
nextBtn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
btn_practicerg.clearCheck(); //line 355
}
});
Logcat
E/AndroidRuntime(729):at
com.example.TEENEINSTIEN.Question$LoadQuestions$2.onCheckedChanged(Question.java:262)
E/AndroidRuntime(729):at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
E/AndroidRuntime(729):at android.widget.RadioGroup.check(RadioGroup.java:166)
E/AndroidRuntime(729):at android.widget.RadioGroup.clearCheck(RadioGroup.java:205)
E/AndroidRuntime(729):at
com.example.TEENEINSTIEN.Question$LoadQuestions$3.onClick(Question.java:355)
E/AndroidRuntime(729):at android.view.View.performClick(View.java:2485)
E/AndroidRuntime(729):at android.view.View$PerformClick.run(View.java:9080)
I try like this, but I can't uncheck..
Upvotes: 34
Views: 130105
Reputation: 19572
I don't have my radio buttons in a group. To clear it from a specific button in Kotlin
use:
binding.whicheverRadioButton.isChecked = false
Of course to check it do the opposite
binding.whicheverRadioButton.isChecked = true
Upvotes: 0
Reputation: 1394
If you have a single radio button and you want to uncheck it on user click, once it was already checked use an extra boolean variable that tracks the checked state of the radio button.
boolean check = false; // initial state of your radio button
myRadioButton.setOnClickListener(view -> {
if (check) {
myRadioButton.setChecked(false);
check = false;
//write code for when button is unchecked
} else {
check = true;
myRadioButton.setChecked(true);
//write code for when button is checked
}
});
Upvotes: 0
Reputation: 832
Below is the code of single radio button
radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!radioButton.isSelected()) {
radioButton.setChecked(true);
radioButton.setSelected(true);
} else {
radioButton.setChecked(false);
radioButton.setSelected(false);
}
}
});
Upvotes: 1
Reputation: 51
if the Radiobutton belong to a radiogroup you can NEVER unchecked alone that button programaticaly, so you must to do a method to unched all first.
public void unchecked()
{
RadioGroup x=findViewById(R.id.NameOfRadiogroup);
x.clearCheck();
}
and then you must to call the method.
Upvotes: 5
Reputation: 976
I think the problem is with btn_practicerg object i.e if you are creating new RadioGroup object everytime you display a new question then you have to execute RadioGroup.clearCheck() on new btn_practicerg object instead of old one that I think you are doing currently.
Even better:
btn_practice1.setText(answ1.get(0));
btn_practice2.setText(answ1.get(1));
btn_practice3.setText(answ1.get(2));
btn_practice4.setText(answ1.get(3));
btn_practice1.setChecked(false);
btn_practice2.setChecked(false);
btn_practice3.setChecked(false);
btn_practice4.setChecked(false);
to uncheck all the buttons in the beginning. I hope this will solve your problem.
Upvotes: 23
Reputation: 61
I had just one radio button and I really didn't want to make a radio button group. So basically I added a radio button then left the text empty, added the checkedTextView next to it (overlapping) then linked the onClick function of the checkedTextView to a toggle function ->
public void toggle(View view)
{
if(checkedTextView.isChecked())
{
checkedTextView.setChecked(false);
actualRadioBtn.setChecked(false);
}
else
{
checkedTextView.setChecked(true);
actualRadioBtn.setChecked(true);
}
}
Then in your function where you check if that radio button is selected you can simply use
if(actualRadioBtn.isChecked())....
Upvotes: 3
Reputation: 51
Try this one, the same problem got solved with this solution:
radio1Clicked
, radio2Clicked
& so onin respective onclick method add below code:
public void radio1Clicked(View view)
{
// Note that I have unchecked radiobuttons except the one
// which is clicked/checked by user
radio2Button.setChecked(false);
radio3Button.setChecked(false);
radio4Button.setChecked(false);
}
simillarly do for remaining radiobuttons. Hope this will solve your problem.
Please feel free to ask any queries regarding the solution.
Upvotes: 3
Reputation: 48252
Put all your buttons in a RadioGroup
then when you need to clear them all use RadioGroup.clearCheck();
Upvotes: 78