lecture slides for preferences and menus [android ]

23
Preferences & Menus Software Development for Portable Devices BITS Pilani Goa Campus Sem I 2011-12

Upload: nehil-jain

Post on 20-May-2015

1.206 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Lecture Slides for Preferences and Menus [Android ]

Preferences & Menus

Software Development for Portable Devices BITS Pilani Goa Campus

Sem I 2011-12

Page 2: Lecture Slides for Preferences and Menus [Android ]

CS C314 Software Development For Portable Devices 2

Foundation of :

Page 3: Lecture Slides for Preferences and Menus [Android ]

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 ]

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 ]

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 ]

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 ]

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 ]

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 ]

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 ]

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 ]

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 ]

Menus

Page 13: Lecture Slides for Preferences and Menus [Android ]

• Types– Options Menu– Context Menu– Submenu

CS C314 Software Development For Portable Devices 13

Page 14: Lecture Slides for Preferences and Menus [Android ]

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 ]

Inflating a Menu Resource

CS C314 Software Development For Portable Devices 15

Page 16: Lecture Slides for Preferences and Menus [Android ]

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 ]

Responding to user action

CS C314 Software Development For Portable Devices 17

Page 18: Lecture Slides for Preferences and Menus [Android ]

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 ]

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 ]

• Same as before

CS C314 Software Development For Portable Devices 20

Responding to user action

Page 21: Lecture Slides for Preferences and Menus [Android ]

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 ]

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 ]

Thank You

CS C314 Software Development For Portable Devices 23