concept PreferenceActivity in category android

appears as: PreferenceActivity, PreferenceActivity
Android in Practice

This is an excerpt from Manning's book Android in Practice.

SharedPreferences also support listeners. You can attach an OnSharedPreferenceChangeListener that acts as a callback to notify you when preferences are changed. We’ll see how that works inside a more useful example where we’ll include a PreferenceActivity.

There are two parts to making the preference activity screen work. There’s a preference resource XML file that defines the elements, and there’s a PreferenceActivity. This is the same arrangement as with a standard layout resource or Activity, but it’s specialized for preferences. We’ll start by examining the XML resource, shown in listing 7.6.

Listing 7.6. The preferences.xml resource file used to define the preference hierarchy

Every preference XML resource starts with a root PreferenceScreen element , and then includes categories with titles and preference elements. There are several types of built-in preference objects including DialogPreference, ListPreference, EditTextPreference, and the one we’re using, CheckBoxPreference . Each preference object has a title, key, value, and summary. Most often the summary is left as static text such as “Enable or disable the splash screen.” We’re going to demonstrate how to make this dynamic and use it to show the current setting (such as in figure 7.6). The other half of all this is the PreferenceActivity class, shown in the following listing.

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest