0% found this document useful (0 votes)
67 views13 pages

c8 - Menus and Dialogs

This document discusses menus and dialogs in Android applications. It covers the different types of menus, including options menus, contextual menus, and popup menus. It also covers the uses of alert dialogs and date/time picker dialogs. Key points include: 1) Menus are used to provide actions and options to users in a consistent way across apps. The options menu contains global actions while contextual menus contain item-specific actions. 2) Alert dialogs can display messages, lists, or custom views and should have no more than three action buttons. Date/Time picker dialogs allow users to select valid dates and times. 3) Fragments are used to host dialogs and ensure they are

Uploaded by

Rheden Gimena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views13 pages

c8 - Menus and Dialogs

This document discusses menus and dialogs in Android applications. It covers the different types of menus, including options menus, contextual menus, and popup menus. It also covers the uses of alert dialogs and date/time picker dialogs. Key points include: 1) Menus are used to provide actions and options to users in a consistent way across apps. The options menu contains global actions while contextual menus contain item-specific actions. 2) Alert dialogs can display messages, lists, or custom views and should have no more than three action buttons. Date/Time picker dialogs allow users to select valid dates and times. 3) Fragments are used to host dialogs and ensure they are

Uploaded by

Rheden Gimena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Mobile Programming

In this module, we will see how Menus and Dialogs work in the Android environment.
We will also learn the different types of Menus and Dialogs that are currently available in the
said environment. And lastly, we will have an idea on when they are viable to use in your
applications.

Objectives:

At the end of the session, the student should be able to:

1. Understand the uses of Menus and Dialogs;

2. Utilize the types of Menus and Dialogs in the android environment;

3. Test and run an android application program that will feature Menus and Dialogs; and

4. Familiarize himself with the terms relevant to the topic;

A. Context and Options Menu

MENUS AND 1
DIALOGS
Menus are a common user interface component in
many types of applications. To provide a familiar and
consistent user experience, you should use the Menu APIs
to present user actions and other options in your activities.

Beginning with Android 3.0 (API level 11),


Android-powered devices are no longer required to provide
a dedicated Menu button. With this change, Android apps
should migrate away from a dependence on the traditional
6-item menu panel and instead provide an action bar to
present common user actions.

Although the design and user experience for some


menu items have changed, the semantics to define a set of
actions and options is still based on the Menu APIs. This
guide shows how to create the three fundamental types of
menus or action presentations on all versions of Android:

Types of Menus in Android

The OptionsMenu is the primary collection of


menu items for an activity. It's where you should place
actions that have a global impact on the app, such as
"Search," "Compose email," and "Settings."

On Android 3.0 and higher, items from the options


menu are presented by the action bar as a combination of
on-screen action items and overflow options. Beginning
with Android 3.0, the Menu button is deprecated (some
devices don't have one), so you should migrate toward
using the action bar to provide access to actions and other
options.
Meanwhile, Tabs work in conjunction with the
ActionBarMenu, which is the bar at the top of all the
examples we’ve been working with thus far. You create
new tabs by requesting one from the ActionBar using its
newTab() method, set its title, give it a listener to respond
to events, and then add it to the ActionBar with its
addTab() method. This is all demonstrated in the following
snippet, which should go in the onCreate() method of
MainActivity.java:
The onTabSelected() callback method gets called
whenever a tab is selected. To display the associated
fragment, all we need to do is tell the ViewPager to change
its currentItem property. You should now be able to select
tabs to change the fragments, but the tabs aren’t updated
when you swipe between them. To fix this, we need to
listen for page changes and update the tabs when the user
swipes, like so:

A ContextualMenu offers actions that affect a


specific item or context frame in the UI. You can provide a
context menu for any view, but they are most often used for
items in a ListView, GridView, or other view collections in
which the user can perform direct actions on each item.

There are two ways to provide contextual actions:

 In a floating context menu. A menu appears as a

floating list of menu items (similar to a dialog) when


the user performs a long-click (press and hold) on a
view that declares support for a context menu. Users
can perform a contextual action on one item at a time.

 In the contextual action mode. This mode is a


system implementation of ActionMode that displays
a contextual action bar at the top of the screen with
action items that affect the selected item(s). When this mode is activ
(Note: The contextual action mode is available on Android 3.0 (API le

A PopupMenu is a modal menu anchored to a View. It


appears below the anchor view if there is room, or above
the view otherwise. It's useful for:

 Providing an overflow-style menu for actions

that relate to specific content

 Providing a second part of a command


sentence (such as a button marked "Add" that produces a popup men
Providing a drop-down similar to that does not retain a persistent sel
(Note: This is not the same as a context menu, which is generally for
or .)

Creating Menu Gro


A menu group is a collection of menu items that
share certain traits. With a group, you can:

 Show or hide all items with setGroupVisible()


 Enable or disable all items with setGroupEnabled()
 Specify whether all items are checkable
with setGroupCheckable()

You can create a group by nesting <item> elements inside a <group> elem
specifying a group ID with the method.

Here's an example menu resource that inc


group:
The items that are in the group appear at the same
level as the first item—all three items in the menu are
siblings. However, you can modify the traits of the two
items in the group by referencing the group ID and using
the methods listed above. The system will also never
separate grouped items. For example, if you
declareandroid: showAsAction= "if Room" for each item,
they will either both appear in the action bar or both appear
in the action overflow.

B. Using the Alert Dialog


A dialog is a small window that prompts the
user to make a decision or enter additional
information. A dialog does not fill the screen and is
normally used for modal events that require users to
take an action before they can proceed.
(Note: The Dialog class is the base class for
dialogs,butyoushouldavoid

instantiating Dialog directly. Instead, use one of the


following subclasses: Alert Dialog and Date/Time
Picker Dialog.)
The AlertDialog class allows you to build a variety
of dialog designs and is often the only dialog class
you'll need. There are three regions of an alert
dialog namely:
1. Title - This is optional and should be used only
when the content area is occupied by a detailed
message, a list, or custom layout. If you need to
state a simple message or question (such as the
dialog in figure 1), you don't need a title.
2. Content area - This can display a message, a
list, or other custom layout.
3. Action buttons - There should be no more than
three action buttons in a dialog.

The Date Picker Dialog and Time Picker


Dialog classes provide reusable interfaces that
ensure a valid date/time is selected by the user.
They also ensure a consistent user interface across
applications.

Creating a date picker requires three things:

1. A DatePickerDialog object defines the


appearance of the dialog.

2. A DialogFragment object hosts the


DatePickerDialog and manages the dialog
lifecycle.

3. An OnDateSetListener
implementation processes the user
input.

The first component is provided by the


Android platform, so all we need to do is instantiate
it.

The DialogFragment is a lightweight


wrapper for the actual dialog, and it makes sure that
the dialog is opened/closed properly, and that any
interruptions are handled correctly. All we need to
do is subclass DialogFragment to return a
DatePickerDialog object as its hosted dialog.
Fragments are introduced in the next chapter, but
for now, suffice it to say that they are modular UI
components. You can think of them as reusable
views that can be embedded in different activities or
dialogs.

To collect the input, we need to implement


the OnDateSetListener interface, which defines a
single method called onDateSet() that gets called
whenever the user closes the dialog. Since you’ll
probably want to process the input in the host
Activity, this is where we’ll define onDateSet().

The showDatePickerDialog() method is


what will open the dialog, but before we get to that,
let’s configure the dialog itself. This is
conventionally done by subclassing
DialogFragment and overriding its
onCreateDialog() method to return whatever dialog
you want to display. So, we need to create a new
class called DatePickerFragment, and it will look
like this:

We used the Calendar class to fetch the


current date, then we extract the year, month, and
day components to give to the date picker. Then, all
we need to do is instantiate DatePickerDialog. The
first parameter should be the host Activity, and the
second one is the listener object, which should
implement the OnDateSetListener interface. Since
we want the host activity to be the listener, too, we
use this as the second parameter (note that this will
require a change to the PickerActivity class
declaration).
Now, we can define the
showDatePickerDialog() method in
PickerActivity.java to display this dialog. Opening
the picker entails creating the DialogFragment
object that hosts the date picker
(DatePickerFragment), then calling its show()
method, like so:

The getSupportFragmentManager() method


is a backwards-compatible way of displaying
fragments (fragments were added in Android 3.0,
but can support back to Android 1.6 using
getSupportFragmentManager()). This method is
defined in FragmentActivity, which means
PickerActivity must subclass that instead of the
usual Activity. Remember that PickerActivity is
also being used as the listener object, so its class
declaration should look like this:

Finally, to process the selected date, we


need to define onDateSet() in PickerActivity.java.
In this case, we’ll just display it in a text field:
(Note: The month parameter is always zero-
indexed to be compatible with the Calendar class.)

Reference:

Rahman K. (2013). Android Development Tools for


Eclipse. (1st ed.) Packt Publishing

Hodson R. (2014). Android Programming


Succinctly (1st ed.) www.syncfusion.com

http://www.developer.android.com

You might also like