3

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);
Coco12
  • 73
  • 1
  • 3
  • 16

2 Answers2

5

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

Strider
  • 4,452
  • 3
  • 24
  • 35
  • Just wondering how to save and load the radio Buttons as couldn't get this last part? For example my strings are currently loaded as follows: strAge = loadSavedPreference("strAge"); And are being saved as follows: savePreference("strAge",strAge); – Coco12 Feb 24 '16 at 17:50
  • You can add that instead, if you want, this was just an example, what this does is saving the state of the radio button **checked/unchecked** and when you load it, it will put the radiobutton back to the last saved state. – Strider Feb 24 '16 at 17:57
  • No as in how to write the "strAge = loadSavedPreference("strAge");" and "savePreference("strAge",strAge);" part for the radio button as oppossed to the string? – Coco12 Feb 24 '16 at 17:59
  • I'm really sorry, but I don't know what u mean/want. Can u try to explain it a bit better? – Strider Feb 24 '16 at 18:21
  • To use those save and load methods I need to call them. For strings I use the line "strAge = loadSavedPreference("strAge");" to save and "savePreference("strAge",strAge);" to load. (You can see this in my original question) However I will need similar lines but for the radio button instead. – Coco12 Feb 24 '16 at 18:58
  • To **save** your radioButtons you only have to call `saveRadioButtons()` and to **load** use `loadRadioButtons()` that's all there is to it. **Note:** I also have editted [this post](http://stackoverflow.com/questions/35604579/how-to-save-spinner-to-saved-shared-preferences/35604884#35604884), it works for your **spinner** and **radiobutton** – Strider Feb 24 '16 at 19:01
2

Inside the onClick() do

sharedPreferences.edit().putBoolean("bntChecked", rdoMale.isChecked()).apply();

And "btnChecked" will be the key that you will use to get the status of your radio button using sharedPreferences

Spirrow
  • 1,120
  • 6
  • 18
  • Glad that you found it helpful! – Spirrow Feb 24 '16 at 12:10
  • Any idea how you'd save a spinner in a similar fashion? – Coco12 Feb 24 '16 at 13:29
  • Use something similar, it will depend on what do you want to save, if it's the value of the spinner or the entry. Have a look at this post to learn how to get the data from the spinner http://stackoverflow.com/questions/1947933/how-to-get-spinner-value – Spirrow Feb 24 '16 at 13:51