Skip to content

Design a Settings or Preference Screen for your app

rutura edited this page Apr 16, 2017 · 2 revisions
  • Android provides a clean and easy to use preferences system for your apps. Using PreferenceActivity or PreferenceFragment, and pair them with settings xml files, you can get good looking and maintainable settings UIs pretty fast.Use them by following the steps below:
    • Organize your settings in some xml file stored in the /res/xml/ folder and format it according to the template below:
    <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <EditTextPreference
        android:key="namePref"
        android:title="Name"
        android:summary="Tell Us Your Name"
        android:defaultValue="Apress" />
    <CheckBoxPreference
        android:key="morePref"
        android:title="Enable More Settings"
        android:defaultValue="false" />
    <PreferenceScreen
        android:key="moreScreen"
        android:title="More Settings"
        android:dependency="morePref">
        <ListPreference
            android:key="colorPref"
            android:title="Favorite Color"
            android:summary="Choose your favorite color"
            android:entries="@array/color_names"
            android:entryValues="@array/color_values"
            android:defaultValue="GRN" />
        <PreferenceCategory
            android:title="Location Settings">
            <CheckBoxPreference
                android:key="gpsPref"
                android:title="Use GPS Location"
                android:summary="Use GPS to Find You"
                android:defaultValue="true" />
            <CheckBoxPreference
                android:key="networkPref"
                android:title="Use Network Location"
                android:summary="Use Network to Find You"
                android:defaultValue="true" />
        </PreferenceCategory>
    </PreferenceScreen>
</PreferenceScreen>
  • Make your Settings Activity extend PreferencesActivity:
    public class MainActivity extends PreferenceActivity {
  • Call addPreferencesFromResource() from your activity context and pass in the location of your settings xml file:
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Load preference data from XML
        addPreferencesFromResource(R.xml.settings);
}
  • Note that our settings xml file in this case is stored at /res/xml/settings.xml
  • With all this in place, the android system takes care of storing and restoring these settings for you even across configuration changes or app restarts.
  • All you have to do in your app is to check the value of the particular setting and do something acordingly:
    SharedPreferences settings =
    PreferenceManager.getDefaultSharedPreferences(this);
    String name = settings.getString("namePref", "");//Do something with name
    boolean isMoreEnabled = settings.getBoolean("morePref", false);
    if (isMoreEnabled)
    { ... }
Clone this wiki locally