Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RadioButton not able to unset using setChecked(false) method

Tags:

If I set a radio button to be selected on the first time, it works fine. But if I unselect it by calling ((RadioButton) findViewById(R.id.ID)).setChecked(false); then, later even if I try to make it selected by calling setChecked(true) will not work unless the user select it from the screen.

Have any one come across this? or is it only me?

        if(Val != null){         if( ((RadioButton) findViewById(R.id.ID1)).getText().toString().trim().equals(Val))         ((RadioButton) findViewById(R.id.ID1)).setChecked(true);         else if(((RadioButton) findViewById(R.id.ID2)).getText().toString().trim().equals(Val))         ((RadioButton) findViewById(R.id.ID2)).setChecked(true);         }         else {             ((RadioButton) findViewById(R.id.ID1)).setChecked(false);             ((RadioButton) findViewById(R.id.ID2)).setChecked(false);         } 

If the else part is executed atleast once then everything gets mess up. When I step thro my debugger, I can see the execution goes in the correct path and setting it to true. It is getting executed only once, I checked that. And I am not resetting it back to false in any other part of the code.

like image 276
franklins Avatar asked Oct 27 '10 16:10

franklins


People also ask

Can you uncheck radio button Android?

You can't uncheck a checked RadioButton. The only way to uncheck is to select some other RadioButton belonging to that RadioGroup. Usually, RadioButtons are used to take mandatory inputs where the user has to choose something and can't leave that field unchecked.

How do I make a radio button not selected by default?

You can check a radio button by default by adding the checked HTML attribute to the <input> element. You can disable a radio button by adding the disabled HTML attribute to both the <label> and the <input> .

How do you check if a RadioButton is checked in a RadioGroup in Android?

here you go. Use getCheckedRadioButtonId() method on your RadioGroup to find out. It returns -1 when no RadioButton in the group is selected. You are already doing this.

How do I make sure only one radio button is selected in Android Studio?

You can use android:checkedButton attribute on RadioGroup, providing the id of the RadioButton you want to be checked initially and selecting another RadioButton will clear the previous selection.


2 Answers

I found the solution.

It is not possible to uncheck a particular radio button. You can only set the other item to true.

So to clear all the checked items, you should call the clearcheck() method on the RadioGroup.

So my else part is

        else {             ((RadioGroup) findViewById(R.id.ID0)).clearCheck();         } 
like image 155
franklins Avatar answered Sep 28 '22 09:09

franklins


Take one invisible radio button and check it. All other radio buttons of group will be unchecked automatically..

like image 39
Kushan Avatar answered Sep 28 '22 10:09

Kushan