Understanding the PreferenceActivity Class

The responsibility of the PreferenceActivity class is to show a hierarchy of Preference objects as lists, possibly spanning multiple screens, as shown in Figure 15-1.

Figure 15-1: The preferences screen for the call settings in Android.

9781118417454-fg1501.eps

When preferences are edited, they’re stored using an instance of Shared Preferences. The SharedPreferences class is an interface for accessing and modifying preference data returned by getSharedPreferences() from any Context object.

A PreferenceActivity is a base class that’s similar to the Activity base class. However, the PreferenceActivity behaves a bit differently. One of the most important features that the PreferenceActivity handles is the displaying of preferences in the visual style that resembles the system preferences. This gives your application a consistent feel across the board in regard to Android user interface components. You should use the PreferenceActivity when dealing with preferences screens in your Android applications.

Persisting preference values

Because the Android framework stores preferences in the Shared Preferences, which automatically stores the preference data in internal storage, you can easily create a preference. When a user edits a preference, the value is automatically saved for you; you don’t have to do any persisting yourself.

Figure 15-2 shows a preference being set in the Task Reminder application. After the user taps OK, Android persists the value to SharedPreferences. Android does all the heavy lifting in regard to persisting the preference values.

Figure 15-2: Setting a preference.

9781118417454-fg1502.tif

Laying out preferences

Working with layouts in Android can sometimes be a painstaking process of alignment, gravity, and other complicating factors. Building layouts is almost like building a website with various tables all over the place. Sometimes it’s easy; sometimes it isn’t. Thankfully, laying out Android preferences is much simpler than defining a layout for the application screen.

Android preferences screens are broken into these categories:

check.png PreferenceScreen: Represents a top-level preference that’s the root of a preference hierarchy. You can use a PreferenceScreen in these two places:

In a PreferenceActivity : The PreferenceScreen isn’t shown because it shows only the containing preferences within the PreferenceScreen definition.

In another preference hierarchy: When present in another hierarchy, the PreferenceScreen serves as a gateway to another screen of preferences (similar to nesting PreferenceScreen declarations inside other PreferenceScreen declarations). Though this concept might seem confusing, you can think of it as XML, where you can declare an element and any element can contain the same parent element. At that point, you’re nesting the elements. The same statement applies to the PreferenceScreen. By nesting PreferenceScreens, you’re informing Android that it should show a new screen when selected.

check.png PreferenceCategory: This preference is used to group preference objects and provide, above the group, a title that describes the category.

check.png Preference: A preference that’s shown onscreen. This preference can be any common preference or a custom one that you define.

By laying out a combination of the PreferenceScreen, Preference Category, and Preference in XML, you can easily create a preferences screen that looks similar to Figure 15-1.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset