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

2

22

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.

Mantellone answered 27/10, 2010 at 16:30 Comment(4)
Could we see your sourcecode ?Clothesline
I tried this android:saveEnabled="false", after going thro groups.google.com/group/android-developers/browse_thread/thread/… , but that didnt help me. Wow atleast I am not alone...Mantellone
Have the same Problem and while my layout is Image - Text - Check Image - Text - Check i can not make only one RadioGroup ... i found that it is a 'bug' when i can not set the Radio with setChecked(false) what the android-developer must do unfix. Thanks for your info, will try it ...Egg
Your Welcome. Did that work for you?Mantellone
M
54

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();
        }
Mantellone answered 27/10, 2010 at 17:42 Comment(3)
Exactly what I needed! It gets really messy with radio buttons sometimes. Thanks!Arin
Or in my case it got clearer when I took off RadioGroup and played with two RadioButtons.Terrellterrena
This solved my issue. I was trying to recheck the already checked radio button which shwoing it as unchecked in UI.Tsana
A
2

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

Audwin answered 15/9, 2013 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.