Working with Android’s Preference Framework

319

Chapter 15: Working with Android’s Preference Framework

➝ 9 The EditTextPreference is a child class of DialogPreference, which means that when you select the preference, you will receive a dialog box similar to what’s 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 is present on the preferences screen, as shown in Figure 15-1. ➝ 12 This line defines the title of the preference on the preference 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 EditTextPreference, which stores the default time in minutes digits that the task reminder time will default 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 presents when the preference is selected. ➝ 19 This line defines the message that will be present in the dialog box. ➝ 20 This line defines the summary of the preference that is present on the main preference screen, as shown in Figure 15-1. ➝ 21 This line defines the title of the preference on the preference screen. Adding string resources For your application to compile, you need the string resources for the prefer- ences. In your resvaluesstrings.xml file, add the following values: -- Preferences -- string name=”pref_category_task_defaults_key”task_default_categorystring string name=”pref_category_task_defaults_title”Task Title Defaultstring string name=”pref_task_title_key”default_reminder_titlestring string name=”pref_task_title_dialog_title”Default Reminder Titlestring 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 Titlestring string name=”pref_category_datetime_key”date_time_default_categorystring string name=”pref_category_datetime_title”Date Time Defaultsstring 320 Part III: Creating a Feature-Rich Application string name=”pref_default_time_from_now_key”time_from_now_defaultstring string name=”pref_default_time_from_now_dialog_title”Time From Nowstring 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 Timestring You should now be able to compile your application. Defining a preference screen was fairly simple — provide the values to the attributes needed and you’re done. While the preference screen may be defined in XML, simply defining it in XML does not mean that it will show up on the screen. To get your preference screen to display on the screen, you need to create a PreferenceActivity. Working with the PreferenceActivity Class The PreferenceActivity shows a hierarchy of preferences on the screen according to a preferences file defined in XML — such as the one you just created. The preferences can span multiple screens if multiple PreferenceScreen objects are present and nested. These preferences automatically save to SharedPreferences. As an added bonus, the prefer- ences shown automatically follow the visual style of the system preferences, which allows your application to have a consistent user experience in con- junction with the default Android platform. To inflate and display the PreferenceScreen you just built, add an activity that derives from PreferenceActivity to your application. I am going to name mine TaskPreferences. The code for this file is shown in Listing 15-2. Listing 15-2: The TaskPreferences File public class TaskPreferences extends PreferenceActivity { ➝ 1 Override protected void onCreateBundle savedInstanceState { super.onCreatesavedInstanceState; addPreferencesFromResourceR.xml.task_preferences; ➝ 5 EditTextPreference timeDefault = EditTextPreference findPreferencegetStringR.string.pref_default_time_from_now_key; ➝ 6 timeDefault.getEditText.setKeyListenerDigitsKeyListener.getInstance; ➝ 7 } } 321

Chapter 15: Working with Android’s Preference Framework