PreferenceFragment
-
You may want your application to provide preferences the users get to set.
-
Example: save login information, how often the app updates external (web) information,
or user may specify a default font size for viewing, etc. - You can use the android SharedPreferences object to same application data like user preferences. (we will explore this more in our module on data)
OPTION 1: Use PreferenceActivity (an Acitivity) class to dispaly activity for user to edit preference
OPTION 2: Since Android 3.0 you can use PreferenceFragment THIS IS EXPLAINED HERE
What is PreferenceFragment?
- a special type of Fragment that displays a hierarchy of preferences to the user.
- specify the preferences in an xml file and use addPreferencesFromResourceMethod() to set
Option 2: PreferenceFragment --an Example
- specify the preferences you want to offer in app to user in xml file preferences.xml (store in res/xml folder)
- create your PreferenceFragment class that extends PreferenceFragment and uses the addPreferencesFromResource(R.xml.preferences) method to load the preferences from
res/xml/preferences.xml
- create your Activity class that extends Activity and use the FragmentManger of the app and its corresponding FragmentTransaction to add or replace an element in your Activity with a new
instance of your Preferencefragement class.
- NOTE: this Activity in your application may have additionally a feature to make the Fragment go away or to store any changes the user may enter in for the preferences
- NOTE: this Activity in your application may have additionally a feature to make the Fragment go away or to store any changes the user may enter in for the preferences
HERE is the preferences file: preferences.xml in res/layout folder
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Category 1">
<CheckBoxPreference android:title="Checkbox" android:defaultValue="false" android:summary="True of False" android:key="checkboxPref" /> </PreferenceCategory> <PreferenceCategory android:title="Category 2"> <EditTextPreference android:name="EditText" android:summary="Enter a string" android:defaultValue="[Enter a string here]" android:title="Edit Text" android:key="editTextPref" /> <RingtonePreference android:name="Ringtone Preference" android:summary="Select a ringtone" android:title="Ringtones" android:key="ringtonePref" /> <PreferenceScreen android:title="Second Preference Screen" android:summary="Click here to go to the second Preference Screen" android:key="secondPrefScreenPref"> <EditTextPreference android:name="EditText" android:summary="Enter a string" android:title="Edit Text (second Screen)" android:key="secondEditTextPref" /> </PreferenceScreen> </PreferenceCategory> </PreferenceScreen>
HERE is the App layout : main.xml in res/layout folder Simple interface --- the TextView will be dynamically replaced by the PreferenceFragment
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
</LinearLayout>HERE is the PreferenceFragmentActivity.java : this uses main.xml as interface and dynamically creates PreferenceFragment and uses it as its content
public class PreferenceFragmentActivity extends Activity { Before Preferences added
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment1 fragment1 = new Fragment1();
fragmentTransaction.replace(android.R.id.content, fragment1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}HERE is the PreferenceFragment class: PreferenceFragement uses R.xml.preferences to specify its preferences
public class Fragment1 extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
//---load the preferences from an XML file--- addPreferencesFromResource(R.xml.preferences); } }