lecture slides for preferences and menus [android ]
TRANSCRIPT
![Page 1: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/1.jpg)
Preferences & Menus
Software Development for Portable Devices BITS Pilani Goa Campus
Sem I 2011-12
![Page 2: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/2.jpg)
CS C314 Software Development For Portable Devices 2
Foundation of :
![Page 3: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/3.jpg)
Saving Simple Application Data
• Shared Preferences• Saved Application State• Files
CS C314 Software Development For Portable Devices 3
![Page 4: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/4.jpg)
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
![Page 5: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/5.jpg)
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
![Page 6: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/6.jpg)
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
![Page 7: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/7.jpg)
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
![Page 8: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/8.jpg)
The Specific Attributes
• android:key• android:title• android:summary• android:defaultValue
CS C314 Software Development For Portable Devices 8
![Page 9: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/9.jpg)
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
![Page 10: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/10.jpg)
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
![Page 11: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/11.jpg)
Preference Activity
• extend the PreferenceActivity class• Call addPreferencesFromResource to inflate
the preferences
CS C314 Software Development For Portable Devices 11
![Page 12: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/12.jpg)
Menus
![Page 13: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/13.jpg)
• Types– Options Menu– Context Menu– Submenu
CS C314 Software Development For Portable Devices 13
![Page 14: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/14.jpg)
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
![Page 15: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/15.jpg)
Inflating a Menu Resource
CS C314 Software Development For Portable Devices 15
![Page 16: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/16.jpg)
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
![Page 17: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/17.jpg)
Responding to user action
CS C314 Software Development For Portable Devices 17
![Page 18: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/18.jpg)
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
![Page 19: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/19.jpg)
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
![Page 20: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/20.jpg)
• Same as before
CS C314 Software Development For Portable Devices 20
Responding to user action
![Page 21: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/21.jpg)
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
![Page 22: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/22.jpg)
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
![Page 23: Lecture Slides for Preferences and Menus [Android ]](https://reader034.vdocuments.mx/reader034/viewer/2022052211/555c3b39d8b42a0b038b4a16/html5/thumbnails/23.jpg)
Thank You
CS C314 Software Development For Portable Devices 23