Jetpad.org

Android Developers

Creating a Preference Activity in Android

The code below shows how to create a class that reads an XML preference definition, and turns it into a usable preferences screen. This functionality is built into Android, and whilst it does not cater for absolutely every case, for the majority of preferences, it should be a good starting point.

import android.os.Bundle;
import android.preference.PreferenceActivity;

 public class Preferences extends PreferenceActivity {
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);  

         addPreferencesFromResource(R.xml.preferences);
     }
 }

Once you have this class, you will need to register it as an activity in AndroidManifest.xml

<activity android:name="Preferences"></activity>

is all that is required.

The next thing you need to do is to create an xml file under res/xml in the project. For the class above, it should be named preferences.xml, although if you change it in the java code above, you should use the same name for the actual file.

This actually permits having multiple xml preferences files, that can be called from different points in the application, whilst only having one class to handle both (or more cases).

The structure of the xml file is relatively straightforward, and I have provided a brief example below.

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

 <PreferenceCategory android:title="Category Name"

 android:summary="Brief summary of category">

 <ListPreference android:key="settingName" android:title="Main text for option"
 android:summary="sub title for option" android:entries="@array/someArrayForDisplayInList"
 android:entryValues="@array/anotherArrayForActualValues" android:dialogTitle="Name of the dialog that pops up" />

 <PreferenceScreen android:title="Main name of sub screen" android:summary="Summary of screen">

 <CheckBoxPreference android:key="settingName"
 android:title="Main Title of Preference"
 android:summary="A brief summary of the option/setting" />

 </PreferenceScreen>

 </PreferenceCategory>

</PreferenceScreen>

There is much more to the xml format than that, but you can see an example of the xml that is available at http://developer.android.com/resources/samples/ApiDemos/res/xml/preferences.html

The only remaining thing to do is to insert the following code where you want to show the preferences

startActivity(new Intent(this, Preferences.class));

Hopefully this can help someone who wants to quickly add preferences/settings to an app they are writing.

Links
The main API page for preferences @ android.com

If you enjoyed this post, make sure you subscribe to my RSS feed!

9 Responses so far.

  1. Dhrumil Shah says:

    Thnx buddy…….
    You have solve my proble……. thank u very much…

  2. Dread says:

    Thank you! Very easy and basic, just what I was looking for, thanks again!!!

  3. Mike says:

    Its half a tutorial, how do I use the preferences once I set them and save them?

  4. Jahid Khan says:

    If you are willing to know details more about “PreferenceActivity” then please read the below link. I get a great help from that tutorial.

    http://www.javacodegeeks.com/2011/01/android-quick-preferences-tutorial.html

  5. Martin says:

    thanks, this is the first manual mentioning the startActivity() function – the last bit because of which my prefs did not work… you saved my app :)

  6. Shahjahan Khan says:

    How to create preference Screen with custom layout and perform action on button.

  7. Silvio says:

    thanks! I finally find what I was looking for in your blog! Nobody tells how to start the Preferences Activity and that the class should be declared in the manifest!

  8. abbasali says:

    thanks but i have a problem :(
    in
    Preference.xml

    in

    for

    @array/…….

    error: Error: No resource found that
    matches the given name (at ‘entries’ with
    value ‘@array/someArrayForDisplayInList’).

    and

    error: Error: No resource found that
    matches the given name (at ‘entryValues’
    with value ‘@array/
    anotherArrayForActualValues’).

Leave a Reply