Preferences & Menus Software Development for Portable Devices BITS Pilani Goa Campus Sem I 2011-12
May 20, 2015
Preferences & Menus
Software Development for Portable Devices BITS Pilani Goa Campus
Sem I 2011-12
CS C314 Software Development For Portable Devices 2
Foundation of :
Saving Simple Application Data
• Shared Preferences• Saved Application State• Files
CS C314 Software Development For Portable Devices 3
Shared Preferences
• Used to store UI state, user preferences, or application settings
• Shared Preferences let you save groups of key/value pairs of primitive data as named preferences.
• They are most commonly used to persist data across user sessions and to share settings among application components.
CS C314 Software Development For Portable Devices 4
Creating And Saving Preferences
• getSharedPreferences(Pref_File_Name,Mode)• Creating new Shared Preferences->Code• Retreiving saved Shared Preferences->Code• Its that Simple• getDefaultSharedPreferences(this)->Gets a
SharedPreferences instance that points to the default file that is used by the preference framework in the given context.
CS C314 Software Development For Portable Devices 5
Preferences Activity Framework
• Android offers an XML-driven framework • Native preference screens.• Having a native feel to your app is important• The Preference Activity framework consists of
three parts:– Preference Screen Layout– Preference Activity– Shared Preference Change Listener
CS C314 Software Development For Portable Devices 6
Preference Screen Layout
• Stored in the res/xml resources folder. – See folder structure
• Each preference layout is defined as a hierarchy, beginning with a single PreferenceScreen element.
• The PreferenceCategory is a category with preferences.
CS C314 Software Development For Portable Devices 7
The Specific Attributes
• android:key• android:title• android:summary• android:defaultValue
CS C314 Software Development For Portable Devices 8
Native Preference Controls
• ListPreference : The preference equivalent of a spinner.
• EditTextPreference : Allows users to enter a string value as a preference.
• CheckBoxPreference :A standard preference checkbox control. Used to set preferences to true or false.
• RingtonePreference : A specialized List Preference that presents the list of available ringtones for user selection.
CS C314 Software Development For Portable Devices 9
Import System Preference Screens
• Invoke any Activity within your Preference Screen using an Intent.
• See Code• The android.provider.Settings class includes a
number of android.settings.* constants that can be used to invoke the system settings screens.
CS C314 Software Development For Portable Devices 10
Preference Activity
• extend the PreferenceActivity class• Call addPreferencesFromResource to inflate
the preferences
CS C314 Software Development For Portable Devices 11
Menus
• Types– Options Menu– Context Menu– Submenu
CS C314 Software Development For Portable Devices 13
Creating a Menu Resource
• Define a menu and all its items in an XML.• Inflate the menu• Location of your menu resource res/menu/
CS C314 Software Development For Portable Devices 14
Inflating a Menu Resource
CS C314 Software Development For Portable Devices 15
Options Menu
• The Menu that’s pops up when you press the menu button
• If there are more than six items in the Options Menu, Android Places the “More” Icon.
CS C314 Software Development For Portable Devices 16
Responding to user action
CS C314 Software Development For Portable Devices 17
Context Menu
• On Android, a context menu is displayed when the user performs a "long press" (press and hold) on an item.
CS C314 Software Development For Portable Devices 18
Context Menu
• In order for a View to provide a context menu, you must "register" the view for a context menu. Call registerForContextMenu(View view)
• View that the user selected • ContextMenu.ContextMenuInfo object that
provides additional information about the item selected.
CS C314 Software Development For Portable Devices 19
• Same as before
CS C314 Software Development For Portable Devices 20
Responding to user action
Attention!
• Join • Course Name CS – IS C314• Official Medium for all the question and
answers• Very Useful while collaborating for projects
and preparing for labs
CS C314 Software Development For Portable Devices 21
References
• Menu Tutorial - http://developer.android.com/guide/topics/ui/menus.html
• Shared Preferenceshttp://developer.android.com/reference/
android/content/SharedPreferences.html
CS C314 Software Development For Portable Devices 22
Thank You
CS C314 Software Development For Portable Devices 23