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 17-1.

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

Figure 17-1: The preference screen for the call settings in Android.

image

A PreferenceActivity is a base class that is very 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 display of preferences in the visual style that resembles the system preferences. This gives your application a feel consistent with other Android user interface components. You use the PreferenceActivity when dealing with preference screens in your Android applications.

Persisting preference values

Because the Android framework stores preferences in the SharedPreferences, which automatically stores the preference data in internal storage, creating a preference is easy. When a user edits a preference, the value is automatically saved for you — that's right, you don't have to do any persisting yourself!

I'm sure this sounds like a little bit of black magic, but I assure you it's not! In Figure 17-2, I am editing an EditTextPreference that will be used in the Task Reminder application. After I select OK, Android takes the value I provided and persists it to SharedPreferences — I don't need to do anything else. Android does all the heavy lifting in regard to persisting the preference values.

Figure 17-2: Setting a preference.

image

Laying out preferences

Working with layouts in Android can sometimes be, well, a painstaking process of alignment, gravity, and so on. Building layouts is almost like building a website with various tables all over the place. Sometimes it's easy; sometimes it's not. Thankfully, laying out Android preferences is much simpler than defining a layout for your application screen.

Android preference screens are broken into the following categories:

  • PreferenceScreen: Represents a top-level preference that is the root of a preference hierarchy. You can use a PreferenceScreen in two places:
    • In a PreferenceActivity: The PreferenceScreen is not shown because it only shows 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. Think of this as nesting PreferenceScreen declarations inside other PreferenceScreen declarations. Although this might seem confusing, think of this as XML. In XML, any element you declare can contain itself as its own parent element. At that point, you're nesting the elements. The same goes for the PreferenceScreen. By nesting them, you are informing Android that it should show a new screen when selected.
  • PreferenceCategory: This preference is used to group preference objects and provide a title above the group that describes the category.
  • Preference: A preference that is shown on the screen. This preference could be any of the common preferences or a custom one that you define.

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

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

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