How To Save Radio Button Status To Saved/shared Preferences?
I am able to save Strings in my saved preferences but having difficulty saving radio buttons. public class PersonalDetailsf extends Activity { private SharedPreferences shared
Solution 1:
Create both a save and a load method:
Save method
publicvoidsaveRadioButtons(){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("Gender", radioSexButton.isChecked());
editor.putBoolean("Male", rdoMale.isChecked());
editor.apply();
}
Load method
publicvoidloadRadioButtons(){
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
Solution 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
Post a Comment for "How To Save Radio Button Status To Saved/shared Preferences?"