Creating Your Preferences Screen

Creating preferences using the PreferenceActivity and a preference XML file is a fairly straightforward process. The first thing you do is create the preference XML file, which defines the layout of the preferences and the string resource values that show up onscreen. These string resources are presented as TextViews onscreen to help the user determine what the preference does.

Your PreferenceScreen should give users the chance to set the default time for a reminder (in minutes) and a default title for a new task. As the application stands now, the default title is empty and the default reminder time is set to the current time. These preferences allow the user to save a couple of steps while building new tasks. For example, if the user normally builds tasks with a reminder time of 60 minutes from the current time, the user can now specify it in the preferences. This new value becomes the value of the reminder time when the user creates a new task.

Building the preferences file

To build your first preferences screen, create a res/xml folder in your project. Inside the res/xml folder, create an XML file and name it task_preferences.xml. Add the code in Listing 15-1 to the file.

Listing 15-1: The task_preferences.xml File

<?xml version=”1.0” encoding=”utf-8”?>

<PreferenceScreen →2

xmlns:android=”http://schemas.android.com/apk/res/android”>

<PreferenceCategory →4

android:key=”@string/pref_category_task_defaults_key” →5

android:title=”@string/pref_category_task_defaults_title”> →6

<EditTextPreference →7

android:key=”@string/pref_task_title_key” →8

android:dialogTitle=”@string/pref_task_title_dialog_title” →9

android:dialogMessage=”@string/pref_task_title_message” →10

android:summary=”@string/pref_task_title_summary” →11

android:title=”@string/pref_task_title_title” /> →12

</PreferenceCategory>

<PreferenceCategory →13

android:key=”@string/pref_category_datetime_key” →14

android:title=”@string/pref_category_datetime_title”> →15

<EditTextPreference →16

android:key=”@string/pref_default_time_from_now_key” →17

android:dialogTitle=”@string/pref_default_time_from_now_dialog_title” →18

android:dialogMessage=”@string/pref_default_time_from_now_message” →19

android:summary=”@string/pref_default_time_from_now_summary” →20

android:title=”@string/pref_default_time_from_now_title” /> →21

</PreferenceCategory>

</PreferenceScreen>

Quite a few string resources are introduced in Listing 15-1 (see the next section). Each numbered line of code is explained as follows:

2 This is the root-level PreferenceScreen; it’s the container for the screen itself. All other preferences live below this declaration.

4 This is a PreferenceCategory that defines the category for task defaults, such as title or body. As you may have noticed, line 13 declares another PreferenceCategory for the default task time. Normally, you place these two items into the same category; they’re split here to show how to use multiple PreferenceCategory elements on one screen.

5 This line defines the key that’s used to store and retrieve the preference from the SharedPreferences. This key must be unique.

6 This line defines the category title.

7 This line contains the definition of the EditTextPreference, which is responsible for storing the preference for the default title of a task.

8 This line contains the key for the default title text EditText Preference.

9 The EditTextPreference is a child class of Dialog Preference. When a user selects the preference, he sees a dialog box similar to the one shown in Figure 15-2. This line of code defines the title for that dialog box.

10 This line defines the message that appears in the dialog box.

11 This line defines the summary text that’s present on the preferences screen, as shown in Figure 15-1.

12 This line defines the title of the preference on the preferences screen.

13 This line defines the PreferenceCategory for the default task time.

14 This line defines the category key.

15 This line defines the title of the category.

16 This line is the start of the definition of the EditText Preference, which stores the default time in minutes (digits) that the task reminder time defaults to from the current time.

17 This line defines the key for the default task time preference.

18 This line defines the title of the dialog box that opens when the preference is selected.

19 This line defines the message that’s present in the dialog box.

20 This line defines the summary of the preference that’s present on the main preferences screen, as shown in Figure 15-1.

21 This line defines the title of the preference on the preferences screen.

Adding string resources

For your application to compile, you need the string resources for the preferences. In the res/values/strings.xml file, add these values:

<!-- Preferences -->

<string name=”pref_category_task_defaults_key”>task_default_category</string>

<string name=”pref_category_task_defaults_title”>Task Title Default</string>

<string name=”pref_task_title_key”>default_reminder_title</string>

<string name=”pref_task_title_dialog_title”>Default Reminder Title</string>

<string name=”pref_task_title_message”>The default title for a reminder.</string>

<string name=”pref_task_title_summary”>Default title for reminders.</string>

<string name=”pref_task_title_title”>Default Reminder Title</string>

<string name=”pref_category_datetime_key”>date_time_default_category</string>

<string name=”pref_category_datetime_title”>Date Time Defaults</string>

<string name=”pref_default_time_from_now_key”>time_from_now_default</string>

<string name=”pref_default_time_from_now_dialog_title”>Time From Now</string>

<string name=”pref_default_time_from_now_message”>The default time from now (in minutes) that a new reminder should be set to.</string>

<string name=”pref_default_time_from_now_summary”>Sets the default time for a reminder.</string>

<string name=”pref_default_time_from_now_title”>Default Reminder Time</string>

You should now be able to compile your application.

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

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