Android Developer Fundamentals Settings UI Preferences Framework Lesson


























































- Slides: 58
Android Developer Fundamentals Settings UI & Preferences Framework Lesson 9 Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 1
9. 2 Settings UI Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 2
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 Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 3
Settings Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 4
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 Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 5
Example settings Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 6
Accessing settings Users access settings through: 1. Navigation drawer 2. Options menu Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 7
Setting screens Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 8
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 Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 9
16+ Settings ● Group into sub-screens opened from main Settings screen Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 10
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 Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 11
Define Settings in a Preference Screen ● Define settings in a preferences screen ● It is like a layout ● define in: res > xml > preferences. xml Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 12
Preference Screen example <Preference. Screen> <Preference. Category android: title="Flight Preferences"> <Check. Box. Preference android: title="Wake for meals". . . /> <Edit. Text. Preference android: title="Favorite city". . . /> </Preference. Category> </Preference. Screen> Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 13
Every Preference must have a key ● Every preference must have a key ● Android uses the key to save the setting value <Edit. Text. Preference android: title="Favorite city" android: key="fav_city" … /> Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 14
Switch. Preference <? xml version="1. 0" encoding="utf-8"? > <Preference. Screen xmlns: android="http: //schemas. android. com/apk/res/android"> <Switch. Preference android: default. Value="true" android: title="@string/pref_title_social" android: key="switch" android: summary="@string/pref_sum_social" /> </Preference. Screen> Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 15
Switch. Preference attributes ● android: default. Value—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 Shared. Preferences Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 16
Edit. Text. Preference <Edit. Text. Preference android: capitalize="words" android: input. Type="text. Cap. Words" android: key="user_display_name" android: max. Lines="1" android: default. Value="@string/pref_default_display_name" android: title="@string/pref_title_display_name" /> Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 17
List. Preference <List. Preference android: default. Value="-1" android: key="add_friends_key" android: entries="@array/pref_example_list_titles" android: entry. Values="@array/pref_example_list_values" android: title="@string/pref_title_add_friends_to_messages" /> Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 18
List. Preference ● Default value of -1 for no choice ● android: entries—Array of labels for radio buttons ● android: entry. Values —Array of values radio button Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 19
Preference class ● Preference class provides View for each kind of setting ● associates View with Shared. Preferences interface to store/retrieve the preference data ● Uses key in the Preference to store the setting value Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 20
Preference subclasses ● Check. Box. Preference—list item that shows a checkbox ● List. Preference—opens a dialog with a list of radio buttons ● Switch. Preference—two-state toggleable option ● Edit. Text. Preference—that opens a dialog with an Edit. Text ● Ringtone. Preference—lets user to choose a ringtone Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 21
Classes for grouping ● Preference. Screen ○ root of a Preference layout hierarchy ○ at the top of each screen of settings ● Preference. Group ○ for a group of settings (Preference objects). ● Preference. Category ○ title above a group as a section divider Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 22
Implement settings Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 23
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 Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 24
Activities and fragments for settings ● Android 3. 0 and newer: ○ App. Compat. Activity with Preference. Fragment. Compat ○ OR use Activity with Preference. Fragment Lesson focusses on this! ● Android older than 3. 0 (API level 10 and lower): ○ build a special settings activity as an extension of the Preference. Activity class (use the template!) Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 25
Steps to implement Settings For App. Compat. Activity with Preference. Fragment. Compat: ● Create the preferences screen ● Create an activity for the settings ● Create a fragment for the settings ● Add the preference. Theme to the App. Theme ● Add code to invoke Settings UI Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 26
Basic Activity template ● Basic Activity template Includes options menu ● Settings menu item provided for options menu Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 27
Create a Settings Activity subclass ● Extends App. Compat. Activity ● in on. Create() display the settings fragment: get. Support. Fragment. Manager(). begin. Transaction(). replace(android. R. id. content, new My. Settings. Fragment()). commit(); Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 28
Settings Activity example public class My. Settings. Activity extends App. Compat. Activity { } @Override protected void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); get. Support. Fragment. Manager(). begin. Transaction(). replace(android. R. id. content, new My. Settings. Fragment()). commit(); } This is the whole class! Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 29
Create a Settings Fragment subclass ● Extends Preference. Fragment. Compat ● Implement methods: ○ on. Create. Preferences() displays the settings ○ set. On. Preference. Change. Listener() handles any changes that need to happen when the user changes a preference (optional) Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 30
Preference. Fragment public class My. Settings. Fragment extends Preference. Fragment. Compat { …} ● Blank fragments include on. Create. View() by default ● Replace on. Create. View() with on. Create. Preferences() because this fragment displays a preferences screen Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 31
Settings Fragment example public class My. Settings. Fragment extends Preference. Fragment. Compat { @Override public void on. Create. Preferences(Bundle saved. Instance. State, String root. Key) { set. Preferences. From. Resource(R. xml. preferences, root. Key); } } Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 32
Add Preference. Theme to app's theme If using Preference. Fragment. Compat, set preference. Theme in styles. xml: <style name="App. Theme" parent=". . . ">. . . <item name="preference. Theme"> @style/Preference. Theme. Overlay </item> … </style> Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 33
Invoke Settings UI Send the Intent to start the Settings Activity: ● From Options menu, update on. Option. Items. Selected() ● From Navigation drawer, update on. Item. Click() on the On. Item. Click. Listener given to set. On. Item. Click. Listener Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 34
Default Settings Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 35
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 Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 36
Set default values ● Use android: default. Value in Preference view in xml: <Edit. Text. Preference android: default. Value="London" … /> ● In main activity's on. Create(), save default values. Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 37
Save default values in shared preferences In on. Create() of Main. Activity Preference. Manager. set. Default. Values( 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 Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 38
Save and retrieve settings Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 39
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 Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 40
Get settings from shared preferences ● In your code, get settings from default shared preferences ● Use key as specified in preference view in xml Shared. Preferences shared. Pref = Preference. Manager. get. Default. Shared. Preferences(this); String destination. Pref = shared. Pref. get. String("fav_city", "Jamaica"); Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 41
Get settings values from shared preferences ● In preference definition in xml: default setting value <Edit. Text. Preference android: default. Value="London" android: key="fav_city" /> ● In code, get fav_city setting: is different than default value returned by pref. get. String() if key is not found in shared prefs String destination. Pref = shared. Pref. get. String("fav_city", "Jamaica"); Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 42
Respond to changes in settings Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 43
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 Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 44
Listen for changes to settings ● Define set. On. Preference. Change. Listener() ● in on. Create() in the Settings Fragment Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 45
on. Create. Preferences() example @Override public void on. Create. Preferences(Bundle saved. Instance. State, String root. Key) { set. Preferences. From. Resource(R. xml. preferences, root. Key); List. Preference color. Pref = (List. Preference) find. Preference("color_pref"); color. Pref. set. On. Preference. Change. Listener( // see next slide //. . . ); } Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 46
on. Preference. Change. Listener() example Example: change background color when setting changes color. Pref. set. On. Preference. Change. Listener( new Preference. On. Preference. Change. Listener(){ @Override public boolean on. Preference. Change( Preference preference, Object new. Value){ set. My. Background. Color(new. Value); return true; } }); Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 47
Summaries for settings Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 48
Summaries for true/false values Set attributes to define conditional summaries for preferences that have true/false values Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 49
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 on. Preference. Change. Listener() Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 50
Set summary example Edit. Text. Preference city. Pref = (Edit. Text. Preference) find. Preference("fav_city"); city. Pref. set. On. Preference. Change. Listener( new Preference. On. Preference. Change. Listener(){ @Override public boolean on. Preference. Change(Preference pref, Object value){ String city = value. to. String(); pref. set. Summary("Your favorite city is " + city); return true; } }); Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 51
Activity Template Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 52
More complex? For anything more complex ? use the Settings. Activity template! Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 53
Settings Activity template ● Complex Settings ● Backwards compatibility Phone ● Customize prepopulated settings ● Adaptive layout for phones and tablets Tablet Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 54
Learn more Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 55
Learn more ● ● ● ● Android Studio User Guide Settings (coding) Preference class Preference. Fragment Shared. Preferences Saving Key-Value Sets Settings (design) Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 56
What's Next? ● Concept Chapter: 9. 2 C App Settings ● Practical: 9. 2 P Adding Settings to an App Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 57
END Android Developer Fundamentals Settings This work is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License 58