Android – How to save radio button status to saved/shared preferences

androidradio-buttonsharedpreferences

I am able to save Strings in my saved preferences but having difficulty saving radio buttons.

public class PersonalDetailsf extends Activity {

    private SharedPreferences sharedPreferences;  

    private RadioGroup radioGroup;
    private RadioButton radioSexButton;
    private RadioButton rdoMale;

Here is my on Create:

    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    String strAge = Integer.toString(age);
    String strHeight = Integer.toString(height);
    String strWeight = Integer.toString(weight);

    name = loadSavedPreference("name");
    strAge = loadSavedPreference("strAge");
    strHeight = loadSavedPreference("strHeight");
    strWeight = loadSavedPreference("strWeight");

    etName.setText(name);
    etAge.setText(strAge);
    etHeight.setText(strHeight);
    etWeight.setText(strWeight);

This is in my onCLick behind a button, where I'm using the radio button and saving the strings:

            Name = etName.getText().toString();
            age = (int) Double.parseDouble(etAge.getText().toString());
            height = (int) Double.parseDouble(etHeight.getText().toString());
            weight = (int) Double.parseDouble(etWeight.getText().toString());


            int selectedId = radioGroup.getCheckedRadioButtonId();

            radioSexButton = (RadioButton) findViewById(selectedId);
            rdoMale = (RadioButton) findViewById(R.id.rdoMale);

            if(rdoMale.isChecked())
            {
                BMR = 10 * weight + 6.25 * height - 5 * age + 5;

            }
                else
            {
                    BMR = 10 * weight + 6.25 * height - 5 * age -161;

            }


            //Save Preferences
            String strAge = Integer.toString(age);
            String strHeight = Integer.toString(height);
            String strWeight = Integer.toString(weight);

            name = etName.getText().toString();
            savePreference("name",name);

            strAge = etAge.getText().toString();
            savePreference("strAge",strAge);

            strHeight = etHeight.getText().toString();
            savePreference("strHeight",strHeight);

            strWeight = etWeight.getText().toString();
            savePreference("strWeight",strWeight);

Best Answer

Create both a save and a load method:

Save method

public void saveRadioButtons(){
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("Gender", radioSexButton.isChecked());
    editor.putBoolean("Male", rdoMale.isChecked());
    editor.apply();
}

Load method

public void loadRadioButtons(){
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    radioSexButton.setChecked(sharedPreferences.getBoolean("Gender", false)); 
    rdoMale.setChecked(sharedPreferences.getBoolean("Male", false));
}

So to save your buttonstates, just call saveRadioButtons()

And to load your buttonstates back, just call loadRadioButtons() somewhere in your code, like your onCreate()

Hope this will help u out