Understanding Check BoxPreference

You saw that the ListPreference preference displays a list as its UI element. Similarly, the CheckBoxPreference preference displays a check-box widget as its UI element.

To extend the flight-search example application, suppose you want to let the user set the list of columns he wants to see with the result set. This preference displays the available columns and allows the user to choose the desired columns by marking the corresponding check boxes. The user interface for this example is shown in Figure 11-3 and the preferences XML file is shown in Listing 11-3.

Airline

Show Airline column

Show Departure column

Show Arrival column

Show Total Travel Time column

Show Price column

Figure 11-3. The user interface for the check-box preference

Listing 11-3. Using a CheckBoxPreference

// chkbox.xml (store at res/xml/chkbox.xml) <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:key="flight_columns_pref" android:title="Flight Search Preferences" android:summary="Set Columns for Search Results"> <CheckBoxPreference android:key="show_airline_column_pref" android:title="Airline" android:summary="Show Airline column" /> <CheckBoxPreference android:key="show_departure_column_pref" android:title="Departure" android:summary="Show Departure column" /> <CheckBoxPreference android:key="show_arrival_column_pref" android:title="Arrival" android:summary="Show Arrival column" /> <CheckBoxPreference android:key="show_total_travel_time_column_pref" android:title="Total Travel Time" android:summary="Show Total Travel Time column" />

<CheckBoxPreference android:key="show_price_column_pref" android:title="Price" android:summary="Show Price column" />

</PreferenceScreen>

// CheckBoxPreferenceActivity.java import android.os.Bundle;

import android.preference.PreferenceActivity;

public class CheckBoxPreferenceActivity extends PreferenceActivity {

^Override protected void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); addPreferencesFromResource(R.xml.chkbox);

Listing 11-3 shows the preferences XML file, chkbox.xml, and a simple activity class that loads it using addPreferencesFromResource(). As you can see, the UI has five check boxes, each of which is represented by a CheckBoxPreference node in the preferences XML file. Each of the check boxes also has a key, which—as you would expect—is ultimately used to persist the state of the UI element when it comes time to save the selected preference. Note that the UI does not define an OK/Cancel button as you saw in the ListPreference example. With the CheckBoxPreference, the state of the preference is saved when the user sets the state. In other words, when the user checks or unchecks the preference control, its state is saved. Listing 11-4 shows the preference data store for this example.

Listing 11-4. The Preferences Data Store for the Check-Box Preference

<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map>

<boolean name="show_total_travel_time_column_pref" value="false" /> <boolean name="show_price_column_pref" value="true" /> <boolean name="show_arrival_column_pref" value="false" /> <boolean name="show_airline_column_pref" value="true" /> <boolean name="show_departure_column_pref" value="false" /> </map>

Again, you can see that each preference is saved through its key attribute. The data type of the CheckBoxPreference is a boolean, which contains a value of either true or false: true to indicate the preference is selected, and false to indicate otherwise. To read the value of one of the check-box preferences, you would get access to the shared preference and then call the getBoolean() method, passing the key of the preference. Now let's have a look at the EditTextPreference.

0 0

Post a comment