6. Building User Interface Menus and pickers
Steps to implement options menu
1. XML menu resource (menu_main.xml) 2. onCreateOptionsMenu() to inflate the menu 3. onClick attribute or onOptionsItemSelected() 4. Method to handle item click
What are fragments?
A Fragment is like a mini-Activity within an Activity
(blank) is Called when users selects an action
onActionItemClicked
(blank) Called when user exits the action mode
onDestroyActionMode
(blank) is always called after onCreateActionMode, but may be called multiple times if action mode is invalidated
onPrepareActionMode
How do you create a popup menu?
1. Create XML menu resource file and assign appearance and position attributes 2. Add ImageButton for the popup menu icon in the XML activity layout file 3. Assign onClickListener to ImageButton 4. Override onClick() to inflate the popup and register it with onMenuItemClickListener() 5. Implement onMenuItemClick() 6. Create a method to perform an action for each popup menu item
AlertDialog can show?
• Title (optional) • Content area • Action buttons
Contextual action mode—temporary action bar in place of or underneath app bar
○ Action items affect the selected View element(s) ○ User can perform action on multiple View elements at once
Floating context menu—long-press on a View
○ User can modify View or use it in some fashion ○ User performs action on one View at a time
Steps for creating floating context menu are?
1. Create XML menu resource file and assign appearance and position attributes 2. Register View using registerForContextMenu() 3. Implement onCreateContextMenu() in Activity to inflate menu 4. Implement onContextItemSelected() to handle menu item clicks 5. Create method to perform action for each context menu item
Steps for contextual action bar are?
1. Create XML menu resource file and assign icons for items 2. setOnLongClickListener() on View that triggers contextual action bar and call startActionMode() to handle click 3. Implement ActionMode.Callback interface to handle ActionMode lifecycle; include action for menu item click in onActionItemClicked() callback 4. Create method to perform action for each context menu item
App Bar at top of each screen for all devices. It contains?
1. Nav icon to open navigation drawer 2. Title of current Activity 3. Icons for options menu items 4. Action overflow button for the rest of the options menu
Dialogs and pickers
1. Alert dialog 2. Date picker 3. Time picker
Types of Menus
1. App bar with options menu 2. Context menu 3. Contextual action bar 4. Popup menu
(blank) appears on top, interrupting flow of Activity and requires user action to dismiss
Dialog
Use (blank) to show a picker, it is a window that floats on top of Activity window
DialogFragment
What has the following properties: • Can be added or removed while parent Activity is running • can be combined in a single Activity • Can be reused in more than one Activity
Fragments
What is Action Mode?
UI mode that lets you replace parts of normal UI interactions temporarily
ActionMode.Callback interface provides lifecycle methods you override:
○ onCreateActionMode(ActionMode, Menu) once on initial creation ○ onPrepareActionMode(ActionMode, Menu) after creation and any time ActionMode is invalidated ○ onActionItemClicked(ActionMode, MenuItem) any time contextual action button is clicked ○ onDestroyActionMode(ActionMode) when action mode is closed
What are contextual menus?
● Allows users to perform action on selected View ● Can be deployed on any View ● Most often used for items in RecyclerView, GridView, or other View collection
Types of contextual menus are?
● Floating context menu—long-press on a View ● Contextual action mode—temporary action bar in place of or underneath app bar
What is a popup menu?
● Vertical list of items anchored to a view ● Typically anchored to a visible icon ● Actions should not directly affect view content