Here are my questions:
- How can I get the value of a switch preference?
- Are things inside a preference fragment already a shared preference?
- If so, how can I access shared preferences from a preference fragment?
Here are my questions:
1. To get the value of a switch:
CompoundButton cb = (CompoundButton)view.findViewById(R.id.myswitch);
if(cb.isChecked())
cb.setChecked(false);
else
cb.setChecked(true);
Reference: Toggle Buttons
2. Yes, the elements in a PreferenceFragment
are automatically stored.
3. To get the SharedPreference
object of a PreferenceFragment
, use:
getDefaultSharedPreferences(Context context)
This is an amazing example on how to make a PreferenceFragment: http://android-er.blogspot.hu/2012/07/example-of-using-preferencefragment.html
Basically the way it works is that you define the preferences with their types and keys. The preference fragment stores its data in the SharedPreferences, which you obtain via Android getDefaultSharedPreferences , and edit its values and obtain them from an Editor.
In a Preference Fragment, you automatically have the data of the prefs linked between the Fragment and the SharedPrefs.
Also look at this example code: http://www.mysamplecode.com/2011/11/android-shared-preferences-example_12.html