Android: How to check/uncheck a RadioButton inside a RadioGroup programmatically in Java

Piezord picture Piezord · Feb 9, 2014 · Viewed 10.3k times · Source

I have to activities, MainActivity and settingsActivity. Because of that I'm using the methods onPause and onResume in the settingsActivity. So that the things I have done in the settingsActivity are saved after switching to MainActivity and back again.

settingsActivity:

Here I have a TextView (called "settings2") as a kind of variable and my three RadioButtons (radio0, radio1 and radio2) which are inside a RadioGroup. After switching to the MainActivity my programe puts "0" in a file (which is saved on the sdcard) if radio0 was last checked. But if radio1 was last checked, it puts 1 in that file. And if radio2 was last checked, it puts 2 in that file. I used this in the method onPause.

Then in the method onResume I read the text of that file and put it in the TextView "settings2". After this code (still in onResume) I want to check/uncheck my RadioButtons. So if the text of the TextView "settings2" is "0" the RadioButton "radio0" shall be checked and the others not. If the text of this TextView is "1" the RadioButton "radio1" shall be checked and the others not. And if the text of this TextView is "2" the RadioButton "radio2" shall be checked and the others not. To do this I used the following two codes but unfortunately they didn't work.

First code:

if (settings2.getText().equals("0")) {

        radio0.setChecked(true);
        radio1.setChecked(false);
        radio2.setChecked(false);

    } else if (settings2.getText().equals("1")) {

        radio0.setChecked(false);
        radio1.setChecked(true);
        radio2.setChecked(false);

    } else if (settings2.getText().equals("2")) {

        radio0.setChecked(false);
        radio1.setChecked(false);
        radio2.setChecked(true);

    } 

Second code:

if (settings2.getText().equals("0")) {

        radioGroup1.check(R.id.radio0);

    } else if (settings2.getText().equals("1")) {

        radioGroup1.check(R.id.radio1);

    } else if (settings2.getText().equals("2")) {

        radioGroup1.check(R.id.radio2);

    } 

Can someone help with this little problem please? I'm looking forward to your help!

Thanks in advance!

Answer

NameSpace picture NameSpace · Feb 9, 2014

Here's the problem.

 EditText et = ....
 et.getText() // Does not return a string, it returns an Editable.

Try:

String str = et.getText().toString;

Then using str to do your comparisons.

Edit: See source code below on why u have to use Strings to compare. The default is to compare by reference, but Strings have overriden equals to compare based on string values. If you aren't using String u won't get matches.

public boolean More ...equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = offset;
            int j = anotherString.offset;
            while (n-- != 0) {
                if (v1[i++] != v2[j++])
                    return false;
            }
            return true;
        }
    }
    return false;
}