In this tutorial we will build a preference screen using the PreferenceActivity class. Our main activity will contain 2 Buttons and a TextView. One of the buttons will open the preference screen, and the other will display the values stored in SharedPreferences.

The final output should look like this:

And here’s what happens when clicking on the Username/Password fields (left screenshot), and the List Preference field (right screenshot):

At a first glance it may look a bit intimidating, but as you will see later, actually it’s quite easy to define a preference screen. Trust me, I’m an engineer! (c) 🙂

To put all this things together in a fashionable way, we will be using the PreferenceActivity class. The good thing about this class is that the definition of layout file it’s very simple. It provides custom controls specially designed for preference screens. Another good thing is that you don’t need to write code to save the values from preference screen to SharedPreferences. All this work is done automatically by the activity.

So, lets begin creating our preference screen

1. Create a new project in Eclipse:

Project: PreferenceDemoTest

Activity: PreferenceDemoActivity

2. Create a new Android XML file prefs.xml in the folder res/xml/. This file will contain the layout for our preference screen:

<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:summary="Username and password information" android:title="Login information" > <EditTextPreference android:key="username" android:summary="Please enter your login username" android:title="Username" /> <EditTextPreference android:key="password" android:summary="Enter your password" android:title="Password" /> </PreferenceCategory> <PreferenceCategory android:summary="Username and password information" android:title="Settings" > <CheckBoxPreference android:key="checkBox" android:summary="On/Off" android:title="Keep me logged in" /> <ListPreference android:entries="@array/listOptions" android:entryValues="@array/listValues" android:key="listpref" android:summary="List preference example" android:title="List preference" /> </PreferenceCategory> </PreferenceScreen>

Notice that the root element of prefs.xml is the <PreferenceScreen> element, and not a RelativeLayout or LinearLayout for example. When a PreferenceActivity points to this layout file, the <PreferenceScreen> is used as the root, and the contained preferences are shown.

<PreferenceCategory> – defines a preference category. In our example the preference screen is split in two categories: “Login Information” and “Settings”

<EditTextPreference> – defines a text field for storing text information.

<CheckBoxPreference> – defines a checkbox.

<ListPreference> – defines a list of elements. The list appears as group of radio buttons.

3. At this stage the prefs.xml might complain that the @array/listOptions and @array/listValues resources cannot be found.

To fix this create a new XML file array.xml in the folder res/values/. This file will contain the elements of the ListPreference.

<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="listOptions"> <item>Option 1</item> <item>Option 2</item> <item>Option 3</item> </string-array> <string-array name="listValues"> <item>1 is selected</item> <item>2 is selected</item> <item>3 is selected</item> </string-array> </resources>

The “listOptions” array defines the elements of the list, or with other words, the labels.

The “listValues” array defines the values of each element. These are the values that will be stored in the SharedPreferences. The number of list options and list values should match. The first list value is assinged to the first list option, the second value to the second option,… and so on.

4. Create a new class PrefsActivity.java that extends PreferenceActivity:

public class PrefsActivity extends PreferenceActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.prefs); } }

Notice that instead of the traditional setContentView(), we use here addPreferencesFromResource() method. This inflates our prefs.xml file and uses it as the Activity’s current layout.

5. Add the PrefsActivity.java to the AndroidManifest file:

<application ......./> <activity android:name=".PrefsActivity" android:theme="@android:style/Theme.Black.NoTitleBar" > </activity> </application>

6. Now, to test our preference activity lets modify the main.xml layout file by adding 2 Buttons and 1 TextView. One of the buttons will open the preference screen, and the other will display the values stored in SharedPreferences.

main.xml:

<?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" > <Button android:id="@+id/btnPrefs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Preferences Screen" /> <Button android:id="@+id/btnGetPreferences" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Display Shared Preferences" /> <TextView android:id="@+id/txtPrefs" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>

7. Finally, modify the PreferenceDemoActivity to handle our logic implementation:

public class PreferenceDemoActivity extends Activity { TextView textView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnPrefs = (Button) findViewById(R.id.btnPrefs); Button btnGetPrefs = (Button) findViewById(R.id.btnGetPreferences); textView = (TextView) findViewById(R.id.txtPrefs); View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnPrefs: Intent intent = new Intent(PreferenceDemoActivity.this, PrefsActivity.class); startActivity(intent); break; case R.id.btnGetPreferences: displaySharedPreferences(); break; default: break; } } }; btnPrefs.setOnClickListener(listener); btnGetPrefs.setOnClickListener(listener); } private void displaySharedPreferences() { SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(PreferenceDemoActivity.this); String username = prefs.getString("username", "Default NickName"); String passw = prefs.getString("password", "Default Password"); boolean checkBox = prefs.getBoolean("checkBox", false); String listPrefs = prefs.getString("listpref", "Default list prefs"); StringBuilder builder = new StringBuilder(); builder.append("Username: " + username + "

"); builder.append("Password: " + passw + "

"); builder.append("Keep me logged in: " + String.valueOf(checkBox) + "

"); builder.append("List preference: " + listPrefs); textView.setText(builder.toString()); } }

Here we attach 2 listeners for each button and display the values retrieved from SharedPreferences in a TextView.

By this time you should compile and run successfully the application.