Overview
Instead of using View
objects to build the user interface, settings is built using various subclasses of the Preference
class That's declare in an XML file.
A Preference
object is the building block for a and a single setting. Each Preference
appears as an item in a list and provides the appropriate UI for users to modify the setting. For example, a CheckBoxPreference
creates a list item this shows a checkbox, and a ListPreference
creates an item that opens a dialog with a LIS T of choices.
Each of you Preference
add have a corresponding Key-value pair that the system uses to save the setting in a default SharedPreferences
file for Your app ' s settings. When the user changes a setting, the system updates the corresponding value in the SharedPreferences
file for you. The only time you should directly interact with the associated file are when you SharedPreferences
need to read the value in order to D Etermine your app ' s behavior based on the user ' s setting.
The value saved in for each setting can is one of the SharedPreferences
following data types:
- Boolean
- Float
- Int
- Long
- String
- String
Set
Because your app ' s settings UI is built using Preference
objects instead View
of objects, you need to use a specialized Activity
or Fragment
subclass to display the list settings:
- If your app supports versions of Android older than 3.0 (API level ten and lower), you must build the activity as an Extens Ion of the
PreferenceActivity
class.
- On Android 3.0 and later, you should instead use a traditional that
Activity
hosts a that PreferenceFragment
displays your app settings. However, you can also use the PreferenceActivity
create a two-pane layout for large screens when you have multiple groups of settings.
How to set up your and instances of are discussed in the PreferenceActivity
PreferenceFragment
sections about Creating a Preference Activity and usin G Preference fragments.
Figure 1. Screenshots from the Android Messaging app ' s settings. Selecting an item defined by a opens a interface to change the Preference
setting.
Preferences
Every setting for your app are represented by a specific subclass of the Preference
class. Each subclass includes a set of core properties of this allow you to specify things such as a title for the setting and the D Efault value. Each subclass also provides its own specialized properties and user interface. For instance, figure 1 shows a screenshot from the Messaging app ' s settings. The list item in the Settings screen is backed by a different Preference
object.
A Few of the most common preferences is:
-
CheckBoxPreference
-
Shows an item with a checkbox for a setting, which is either enabled or disabled. The saved value is a Boolean (
true
if it ' s checked).
-
ListPreference
-
Opens a dialog with a list of radio buttons. The saved value can be any one of the supported value types (listed above).
-
EditTextPreference
-
Opens A dialog with a
EditText
widget. The saved value is a
String
.
See the Preference
class for a list of all other subclasses and their corresponding properties.
Of course, the built-in classes don ' t accommodate every need and your application might require something more specialized . For example, the platform currently does not provide a Preference
class for picking a number or a date. Might need to define your own Preference
subclass. For help doing so, see the section aboutbuilding a Custom Preference.
Android Preferences (1) Overview and preferences Introduction