App Settings
App Settings
Preferences
and settings
Lesson 9
Pose the least risk to security and data loss. For example, the default setting for the Gmail
app's default action is to archive rather than delete messages.
Interrupt only when important. For example, the default setting for when calls and
notifications arrive is to interrupt only when important
Note: Use the word Settings in the app's navigation to access the settings. Do not use
synonyms such as "Options" or "Preferences."
Each Preference appears as an item in a list. Direct subclasses provide containers for layouts involving multiple settings.
For example:
PreferenceScreen: Represents a top-level Preference that is the root of a Preference hierarchy. Use a PreferenceScreen in
a layout at the top of each screen of settings.
<SwitchPreference
android:defaultValue="true"
android:title="@string/pref_title_social"
android:key="switch"
android:summary="@string/pref_sum_social" />
</PreferenceScreen>
● android:defaultValue—true by default
● android:summary—text underneath setting, for some
settings, should change to reflect value
● android:title—title/name
● android:key—key for storing value in SharedPreferences
<EditTextPreference
android:capitalize="words"
android:inputType="textCapWords"
android:key="user_display_name"
android:maxLines="1"
android:defaultValue="@string/pref_default_display_name"
android:title="@string/pref_title_display_name" />
<ListPreference
android:defaultValue="-1"
android:key="add_friends_key"
android:entries="@array/pref_example_list_titles"
android:entryValues="@array/pref_example_list_values"
android:title="@string/pref_title_add_friends_to_messages" />
● PreferenceGroup
○ for a group of settings (Preference objects).
● PreferenceCategory
○ title above a group as a section divider
This work is licensed under a Creative
Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 25
License.
Implement
settings
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, new MySettingsFragment())
.commit();
} This is the
} whole class!
In onCreate() of MainActivity
PreferenceManager.setDefaultValues(
this, R.xml.preferences, false);
● App context, such as this
● Resource ID of XML resource file with settings
● false only calls method the first time the app starts
String destinationPref =
sharedPref.getString("fav_city", "Jamaica");
This work is licensed under a Creative
Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 45
License.
Respond to
changes in
settings