Выбрать главу

 switch (item.getItemId()) {

  case EDIT_ID:

   startActivity(new Intent(this, EditPreferences.class));

   return(true);

  case CLOSE_ID:

   finish();

   return(true);

 }

 return(super.onOptionsItemSelected(item));

}

However, that is all that is needed, and it really is not that much code outside of the preferences XML. What you get for your effort is an Android-supplied preference UI, as shown in Figure 17-1.

Figure 17-1. The Simple project’s preferences UI

The checkbox can be directly checked or unchecked. To change the ringtone preference, just click on the entry in the preference list to bring up a selection dialog like the one in Figure 17-2.

Figure 17-2. Choosing a ringtone preference

Note that there is no explicit Save or Commit button or menu — changes are persisted as soon as they are made.

The SimplePrefsDemo activity, beyond having the aforementioned menu, also displays the current preferences via a TableLayout:

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

<TableLayout

 xmlns:android="http://schemas.android.com/apk/res/android"

 android:layout_width="fill_parent"

 android:layout_height="fill_parent"

>

 <TableRow>

  <TextView

   android:text="Checkbox:"

   android:paddingRight="5px"

  />

  <TextView android:id="@+id/checkbox"

  />

 </TableRow>

 <TableRow>

  <TextView

   android:text="Ringtone:"

   android:paddingRight="5px"

  />

  <TextView android:id="@+id/ringtone"

  />

 </TableRow>

</TableLayout>

The fields for the table are found in onCreate():

@Override

public void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);

 setContentView(R.layout.main);

 checkbox = (TextView)findViewById(R.id.checkbox);

 ringtone = (TextView)findViewById(R.id.ringtone);

}

The fields are updated on each onResume():

@Override

public void onResume() {

 super.onResume();

 SharedPreferences prefs = PreferenceManager

  .getDefaultSharedPreferences(this);

 checkbox.setText(new Boolean(prefs

  .getBoolean("checkbox", false)).toString());

 ringtone.setText(prefs.getString("ringtone", "<unset>"));

}

This means the fields will be updated when the activity is opened and after the preferences activity is left (e.g., via the back button); see Figure 17-3.

Figure 17-3. The Simple project’s list of saved preferences

Adding a Wee Bit o’ Structure

If you have a lot of preferences for users to set, having them all in one big list may become troublesome. Android’s preference framework gives you a few ways to impose a bit of structure on your bag of preferences, including categories and screens.

Categories are added via a PreferenceCategory element in your preference XML and are used to group together related preferences. Rather than have your preferences all as children of the root PreferenceScreen, you can put a few PreferenceCategory elements in the PreferenceScreen, and then put your preferences in their appropriate categories. Visually, this adds a divider with the category title between groups of preferences.

If you have lots and lots of preferences — more than is convenient for users to scroll through — you can also put them on separate “screens” by introducing the PreferenceScreen element.

Yes, that PreferenceScreen element.

Any children of PreferenceScreen go on their own screen. If you nest PreferenceScreens, the parent screen displays the screen as a placeholder entry — tapping that entry brings up the child screen. For example, from the Prefs/Structured sample project on the Apress Web site, here is a preference XML file that contains both PreferenceCategory and nested PreferenceScreen elements:

<PreferenceScreen

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

 <PreferenceCategory android:title="Simple Preferences">

  <CheckBoxPreference

   android:key="@string/checkbox"

   android:title="Checkbox Preference"

   android:summary="Check it on, check it off"

  />

  <RingtonePreference

   android:key="@string/ringtone"

   android:title="Ringtone Preference"

   android:showDefault="true"

   android:showSilent="true"

   android:summary="Pick a tone, any tone"

  />

 </PreferenceCategory>

 <PreferenceCategory android:title="Detail Screens">

  <PreferenceScreen

   android:key="detail"

   android:title="Detail Screen"

   android:summary="Additional preferences held in another page">

   <CheckBoxPreference

    android:key="@string/checkbox2"

    android:title="Another Checkbox"

    android:summary="On. Off. It really doesn't matter."

   />

  </PreferenceScreen>

 </PreferenceCategory>

</PreferenceScreen>

The result, when you use this preference XML with your PreferenceActivity implementation, is a categorized list of elements like those in Figure 17-4.

Figure 17-4. The Structured project’s preference UI, showing categories and a screen placeholder