Chapter 15

Working with Android’s Preferences Framework

In This Chapter

arrow Seeing how preferences work in Android

arrow Building a preferences screen

arrow Working with preferences programmatically

Most programs need to be configured to suit a user’s needs (for the most part), with individual settings or preferences. Allowing users to configure your Android application gives it a usability advantage. Thankfully, creating and providing a mechanism to edit preferences in Android are fairly easy processes.

Android provides, out of the box, a robust preferences framework that lets you declaratively — and programmatically — define preferences for your application. Android stores preferences as persistent key-value pairs of primitive data types for you. You aren’t required to store the values in a file or database or in any other mechanism. The Android preferences framework commits the values you provide to internal storage on behalf of your application. You can use the preferences framework to store Boolean, float, int, long, and string elements. The data persists across user sessions — if the user closes the app and reopens it later, the preferences are saved and can be used, even if your application is killed.

This chapter delves into the Android preferences framework and describes how to incorporate it into your applications. You find out how to use the built-in PreferenceActivity to create and edit preferences and how to read and write preferences from code within your application. At the end of this chapter, you’ll have integrated preferences fully into the Task Reminder application.

tip.eps If you’re using Android 3.x or later and you don’t care to support older devices, consider using the new PreferenceFragment instead of Preference Activity. However, the PreferenceFragment isn’t yet available as part of the support library, so you cannot use PreferenceFragments on older devices. For this reason, this chapter sticks with using the tried-and-true PreferenceActivity.

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

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