Reputation: 473
I have made 4 radio buttons and want to save the state when any of them is clicked and then want to use that saved state in the application.How can i do that?
myOption1.setChecked(true);
myOption2.setChecked(true);
myOption3.setChecked(true);
myOption4.setChecked(true);
Upvotes: 3
Views: 6826
Reputation: 10622
Save that value in sharedPreferences Concept. Sample Example has been placed here that uses predefined method called addPreferencesFromResource() that will get the state of checkbox value automatically and saved in the preferences.
public class EditPreference extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.checkboxpref);
addPreferencesFromResource(R.layout.checkboxpref);
}
}
And layout Code should be this :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="@string/checkbox_pref"
android:title="@string/eidt_preferences">
<CheckBoxPreference
android:key="@string/pref_12_time_format"
android:title="@string/time_formats"
android:summary="@string/twelve_hour"
android:defaultValue="true"/>
<CheckBoxPreference
android:key="@string/pref_show_current_location"
android:title="@string/show_current_location"
android:summary="@string/using_gps_network"
android:defaultValue="true"/>
<CheckBoxPreference
android:key="@string/pref_display_date"
android:title="@string/display_date"
android:defaultValue="true"/>
</PreferenceScreen>
Now you have retreive this stored checkbox value has follows :
SharedPreferences sharedPreference = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
mTimeFormat = sharedPreference.getBoolean("pref_12_time_format", false);
mServiceType = sharedPreference.getBoolean(
"pref_show_current_location", false);
mDisplayDate = sharedPreference.getBoolean("pref_display_date", false);
Upvotes: 0
Reputation: 6071
If you're only going to have 4 radio buttons all in all, you might as well store their "values" in SharedPreferences
(persistant storage).
Example:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor shEditor = sharedPreferences.edit();
shEditor.putBoolean("checkbox_1", myOptionOne.isChecked());
shEditor.putBoolean("checkbox_2", myOptionTwo.isChecked());
shEditor.putBoolean("checkbox_3", myOptionThree.isChecked());
shEditor.putBoolean("checkbox_4", myOptionFour.isChecked());
shEditor.commit();
Use it by doing the following:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
myOptionOne.setChecked(sharedPreferences.getBoolean("checkbox_1", false));
myOptionTwo.setChecked(sharedPreferences.getBoolean("checkbox_2", false));
myOptionThree.setChecked(sharedPreferences.getBoolean("checkbox_3", false));
myOptionFour.setChecked(sharedPreferences.getBoolean("checkbox_4", false));
Upvotes: 4
Reputation: 13247
Override onSaveInstanceState() and onRestoreInstanceState() in your activity.
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean("myOption1", myOption1.isChecked());
savedInstanceState.putBoolean("myOption2", myOption2.isChecked());
savedInstanceState.putBoolean("myOption3", myOption3.isChecked());
savedInstanceState.putBoolean("myOption4", myOption4.isChecked());
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
myOption1.setChecked(savedInstanceState.getBoolean("myOption1"));
myOption2.setChecked(savedInstanceState.getBoolean("myOption2"));
myOption3.setChecked(savedInstanceState.getBoolean("myOption3"));
myOption4.setChecked(savedInstanceState.getBoolean("myOption4"));
}
Upvotes: 5