Creating A Standard Preference Activity For The Earthquake Viewer

Previously in this chapter you created a custom Activity to let users modify the application settings for the earthquake viewer. In this example you'll replace this custom Activity with the standard application settings framework described in the previous section.

1. Start by creating a new XML resource folder at res/xml. Within it create a new userprefer-ences.xml file. This file will define the settings UI for your earthquake application settings. Use the same controls and data sources as in the previous Activity, but this time create them using the standard application settings framework.

Be sure to use the preference keys you defined earlier.

<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android=""> <CheckBoxPreference android:key="PREF_AUTO_UPDATE" android:title="Auto refresh"

android:summary="Select to turn on automatic updating" android:defaultValue="true"

<ListPreference android:key="PREF_UPDATE_FREQ" android:title="Refresh frequency"

android:summary="Frequency at which to refresh earthquake list" android:entries="@array/update_freq_options" android:entryValues="@array/update_freq_values" android:dialogTitle="Refresh frequency"


<ListPreference android:key="PREF_MIN_MAG" android:title="Minimum magnitude"

android:summary="Select the minimum magnitude earthquake to report"






2. Open the Preference Activity and modify its inheritance to extend PreferenceActivity: public class UserPreferences extends PreferenceActivity

3. The Preference Activity will handle the controls used in the UI, so you can remove the variables used to store the checkbox and spinner objects. You can also remove the populateSpinners, updateUIFromPreferences, and savePreferences methods.

4. Now update onCreate. Remove all the references to the UI controls and the OK and Cancel buttons. Instead of using these, inflate the preferences UI file you created in Step 1:

©Override public void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); addPreferencesFromResource(R.xml.preferences);

5. If you run your application now, and select the

Preferences menu item, your new "native" settings screen should be visible, as shown in Figure 6-4.

All code snippets in this example are part of the Chapter 6 Earthquake Part 2 project, available for download at

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment