0% found this document useful (0 votes)
36 views61 pages

App Settings

This document discusses app settings in Android. It defines settings as values that change infrequently and are relevant to most users. It recommends using the Preference class to build settings and store values in SharedPreferences. It also provides guidelines for organizing settings screens, common preference types like SwitchPreference and EditTextPreference, and best practices for navigating to and designing settings screens.

Uploaded by

Arjun Ramavath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views61 pages

App Settings

This document discusses app settings in Android. It defines settings as values that change infrequently and are relevant to most users. It recommends using the Preference class to build settings and store values in SharedPreferences. It also provides guidelines for organizing settings screens, common preference types like SwitchPreference and EditTextPreference, and best practices for navigating to and designing settings screens.

Uploaded by

Arjun Ramavath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Android Developer Fundamentals V2

Preferences
and settings
Lesson 9

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 1
License.
9.2 App settings

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 2
License.
Contents
● What are settings?
● Setting screens
● Implement settings
● Default settings
● Save and retrieve settings
● Respond to changes in settings
● Summaries for settings
● Settings Activity template
This work is licensed under a Creative
Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 3
License.
Settings

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 4
License.
What are app settings?

● Users can set features and behaviors of app


Examples:
○ Home location, defaults units of measurement
○ Notification behavior for specific app

● For values that change infrequently and are relevant to


most users
● If values change often, use options menu or nav drawer
This work is licensed under a Creative
Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 5
License.
The initial default value for settings
Represent the value most users would choose, such as All contacts for "Contacts to
display" in the Contacts app.
Use less battery power. For example, in the Android Settings app, Bluetooth is set to off
until the user turns it on.

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

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 6
License.
Example settings

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 7
License.
Accessing settings
Users access settings
through:
1. Navigation drawer
2. Options menu

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 8
License.
Design guidelines for navigating to settings
If your app offers side navigation such as a navigation drawer, include Settings below all
other items (except Help and Send Feedback).
If your app doesn't offer side navigation, place Settings in the app bar menu's options menu
below all other items (except Help and Send Feedback).

Note: Use the word Settings in the app's navigation to access the settings. Do not use
synonyms such as "Options" or "Preferences."

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 9
License.
Setting screens

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 10
License.
Organize your settings

● Predictable, manageable number of


options
● 7 or less: arrange according to priority
with most important at top
● 7-15 settings: group related settings
under section dividers

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 11
License.
16+ Settings

● Group into screens


opened from main
Settings screen

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 12
License.
Building the settings
Build an app's settings using various subclasses of the Preference class rather than using View objects. This class
provides the View to be displayed for each setting, and associates with it a SharedPreferences interface to store/retrieve
the preference data.

Each Preference appears as an item in a list. Direct subclasses provide containers for layouts involving multiple settings.
For example:

PreferenceGroup: Represents a group of settings (Preference objects).

PreferenceCategory: Provides a disabled title above a group as a section divider.

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.

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 13
License.
View versus Preference
● Use Preference objects instead of View objects in your
Settings screens
● Design and edit Preference objects in the layout editor
just like you do for View objects

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 14
License.
Define Settings in a Preference Screen
● Define settings in a
preferences screen
● It is like a layout
● define in:
res > xml > preferences.xml

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 15
License.
Preference Screen example
<PreferenceScreen>
<PreferenceCategory
android:title="Flight Preferences">
<CheckBoxPreference
android:title="Wake for meals"
... />
<EditTextPreference
android:title="Favorite city"
.../>
</PreferenceCategory>
</PreferenceScreen>
This work is licensed under a Creative
Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 16
License.
Every Preference must have a key
● Every preference must have a key
● Android uses the key to save the setting value
<EditTextPreference
android:title="Favorite city"
android:key="fav_city"
… />
This work is licensed under a Creative
Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 17
License.
SwitchPreference
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">

<SwitchPreference
android:defaultValue="true"
android:title="@string/pref_title_social"
android:key="switch"
android:summary="@string/pref_sum_social" />
</PreferenceScreen>

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 18
License.
SwitchPreference attributes

● 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

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 19
License.
EditTextPreference

<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" />

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 20
License.
ListPreference

<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" />

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 21
License.
ListPreference

● Default value of -1 for no choice


● android:entries—Array of labels for radio buttons
● android:entryValues —Array of values radio button

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 22
License.
Preference class

● Preference class provides View for each kind of setting


● associates View with SharedPreferences interface to
store/retrieve the preference data
● Uses key in the Preference to store the setting value

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 23
License.
Preference subclasses

● CheckBoxPreference—list item that shows a checkbox


● ListPreference—opens a dialog with a list of radio buttons
● SwitchPreference—two-state toggleable option
● EditTextPreference—that opens a dialog with an EditText
● RingtonePreference—lets user to choose a ringtone

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 24
License.
Classes for grouping
● PreferenceScreen
○ root of a Preference layout hierarchy
○ at the top of each screen of settings

● 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

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 26
License.
Settings UI uses fragments
● Use an Activity with a Fragment to display the Settings
screen
● Use specialized Activity and Fragment subclasses that
handle the work of saving settings

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 27
License.
Activities and fragments for settings
● Android 3.0 and newer: Lesson
focusses
○ AppCompatActivity with PreferenceFragmentCompat on this!
○ OR use Activity with PreferenceFragment

● Android older than 3.0 (API level 10 and lower):


○ build a special settings activity as an extension of the
PreferenceActivity class (use the template!)

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 28
License.
Steps to implement Settings
For AppCompatActivity with PreferenceFragmentCompat:
● Create the preferences screen
● Create an Activity for the settings
● Create a Fragment for the settings
● Add the preferenceTheme to the AppTheme
● Add code to invoke Settings UI
This work is licensed under a Creative
Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 29
License.
Basic Activity template

● Basic Activity template


Includes options menu
● Settings menu item provided
for options menu

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 30
License.
Create a Settings Activity subclass
● Extends AppCompatActivity
● in onCreate() display the settings Fragment:
getSupportFragmentManager()
.beginTransaction()
.replace(android.R.id.content,
new MySettingsFragment())
.commit();
This work is licensed under a Creative
Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 31
License.
Settings Activity example
public class MySettingsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, new MySettingsFragment())
.commit();
} This is the
} whole class!

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 32
License.
Create a Settings Fragment subclass
● Extends PreferenceFragmentCompat
● Implement methods:
○ onCreatePreferences() displays the settings
○ setOnPreferenceChangeListener() handles any
changes that need to happen when the user changes a
preference (optional)

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 33
License.
PreferenceFragment

public class MySettingsFragment


extends PreferenceFragmentCompat { …}

● Blank fragments include onCreateView() by default


● Replace onCreateView() with onCreatePreferences()
because this fragment displays a preferences screen

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 34
License.
Settings Fragment example
public class MySettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState,
String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
}

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 35
License.
Add PreferenceTheme to app's theme
If using PreferenceFragmentCompat, set preferenceTheme
in styles.xml:
<style name="AppTheme" parent="...">
...
<item name="preferenceTheme">
@style/PreferenceThemeOverlay
</item>

</style>

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 36
License.
Invoke Settings UI
Send the Intent to start the Settings Activity:
● From Options menu, update onOptionItemsSelected()
● From Navigation drawer, update onItemClick() on the
OnItemClickListener given to setOnItemClickListener

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 37
License.
Default Settings

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 38
License.
Default settings
● Set default to value most users would choose
○ All contacts
● Use less battery power
○ Bluetooth is off until the user turns it on
● Least risk to security and data loss
○ Archive rather than delete messages
● Interrupt only when important
○ When calls and notifications arrive
This work is licensed under a Creative
Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 39
License.
Set default values
● Use android:defaultValue in Preference view in xml:
<EditTextPreference
android:defaultValue="London"
… />
● In onCreate() of MainActivity, save default values.

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 40
License.
Save default values in shared preferences

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

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 41
License.
Save and retrieve
settings

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 42
License.
Saving setting values
● No need to write code to save settings!
● If you use specialized Preference Activity and Fragment,
Android automatically saves setting values in shared
preferences

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 43
License.
Get settings from shared preferences
● In your code, get settings from default shared preferences
● Use key as specified in preference view in xml
SharedPreferences sharedPref =
PreferenceManager.getDefaultSharedPreferences(this);
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 44
License.
Get settings values from shared preferences

● In preference definition in xml: default setting value


<EditTextPreference is different than
android:defaultValue="London"
android:key="fav_city" /> default value returned by
pref.getString() if key is
● In code, get fav_city setting: not found in shared prefs

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

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 46
License.
Listening to changes

● Display related follow-up settings


● Disable or enable related settings
● Change the summary to reflect current choice
● Act on the setting
For example, if the setting changes the screen
background, then change the background
This work is licensed under a Creative
Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 47
License.
Listen for changes to settings
● Define setOnPreferenceChangeListener()
● in onCreatePreferences() in the Settings Fragment

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 48
License.
onCreatePreferences() example
@Override
public void onCreatePreferences(Bundle savedInstanceState,
String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
ListPreference colorPref =
(ListPreference) findPreference("color_pref");
colorPref.setOnPreferenceChangeListener(
// see next slide
// ...);
}
This work is licensed under a Creative
Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 49
License.
onPreferenceChangeListener() example
Example: change background color when setting changes
colorPref.setOnPreferenceChangeListener(
new Preference.OnPreferenceChangeListener(){
@Override
public boolean onPreferenceChange(
Preference preference, Object newValue){
setMyBackgroundColor(newValue);
return true;
}
});
This work is licensed under a Creative
Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 50
License.
Summaries for
settings

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 51
License.
Summaries for true/false values
Set attributes to define
conditional summaries for
preferences that have
true/false values

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 52
License.
Summaries for other settings
For settings that have values other than true/false, update
the summary when the setting value changes
● Set the summary in onPreferenceChangeListener()

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 53
License.
Set summary example
EditTextPreference cityPref = (EditTextPreference)
findPreference("fav_city");
cityPref.setOnPreferenceChangeListener(
new Preference.OnPreferenceChangeListener(){
@Override
public boolean onPreferenceChange(Preference pref, Object value){
String city = value.toString();
pref.setSummary("Your favorite city is " + city);
return true;
}
});
This work is licensed under a Creative
Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 54
License.
Activity
Template

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 55
License.
More complex?

For anything more complex


?
use the Settings Activity template!

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 56
License.
Settings Activity template
● Complex Settings
● Backwards
compatibility Phone
● Customize pre-
populated settings
● Adaptive layout for
phones and tablets Tablet
This work is licensed under a Creative
Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 57
License.
Learn more

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 58
License.
Learn more
● Android Studio User Guide
● Settings (coding)
● Preference class
● PreferenceFragment
● Fragment
● SharedPreferences
● Saving Key-Value Sets
● Settings (design)
This work is licensed under a Creative
Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 59
License.
What's Next?

● Concept Chapter: 9.2 App settings


● Practical: 9.2 App settings

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 60
License.
END

This work is licensed under a Creative


Android Developer Fundamentals V2 App settings Commons Attribution 4.0 International 61
License.

You might also like