20.08.2016 Views

Professional Android 4 Application Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Creating a Standard Preference Activity for the Earthquake Viewer x 239<br />

This example describes two ways of creating a Preference Activity — first, using the legacy<br />

PreferencesActivity, and then a backward-compatible alternative using the newer<br />

PreferenceFragment techniques.<br />

1. Start by creating a new XML resource folder at res/xml. Within it create a new<br />

userpreferences.xml file. This file will define the settings UI for your earthquake<br />

application settings. Use the same controls and data sources as in the previous Activity, but<br />

this time create them using the standard application settings framework. Note that in this<br />

example difference key names are selected. This is because where you were previously recording<br />

integers, you’re now recording strings. To avoid type mismatches when the application<br />

attempts to read the saved preferences, use a different key name.<br />

<br />

<br />

<br />

<br />

<br />

<br />

2. Open the PreferencesActivity Activity and modify its inheritance to extend<br />

PreferenceActivity:<br />

public class PreferencesActivity extends PreferenceActivity {<br />

3. The Preference Activity will handle the controls used in the UI, so you can remove the<br />

variables used to store the check box and spinner objects. You can also remove the populateSpinners,<br />

updateUIFromPreferences, and savePreferences methods. Update the<br />

preference name strings to match those used in the user preferences definition in step 1.<br />

public static final String PREF_MIN_MAG = “PREF_MIN_MAG”;<br />

public static final String PREF_UPDATE_FREQ = “PREF_UPDATE_FREQ”;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!