Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

SLIDEBAZAAR |

Menus, Dialogues, Lists

SLIDEBAZAAR |
• Appear whenever the user presses the menu button.
• Useful for giving different options without leaving the current Activity
• Don’t make too big menus, or they’ll cover entirely the Activity
• There are 3 types :
Options menu
→ 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."
Popup menu
→ Displays a list of items in a vertical list that's anchored
to the view that invoked the menu.
Context menu
→ Is a floating menu that appears when the user
performs a long-click on an element.
→ It provides actions that affect the selected content or
context frame.
SLIDEBAZAAR |
• XML
Two methods  Place a file inside res/menu/
 Inflate the menu inside the Activity
(again)  Useful if you want to create the same menu
inside different activities
• Java : Create the menu directly inside the activity

o For all menu types, Android provides a standard XML format to define menu items.
o Define a menu and all its items in an XML menu resource then inflate the menu
resource (load it as a Menu object) in activity.
Defining a o Using a menu resource is a good practice for a few reasons:
Menu in → It's easier to visualize the menu structure in XML.
XML → It separates the content for the menu from application's behavioral code.
→ It allows to create alternative menu configurations for different platform
versions, screen sizes, and other configurations by leveraging the app
SLIDEBAZAAR |
resources framework.
→ To define the menu, create an XML file
inside project's res/menu/ directory and build menu.xml
the menu with the following elements:
• <menu>
• Defines a Menu, which is a container for menu items.
• A <menu> element must be the root node for the file
and can hold one or
more <item> and <group> elements.
• <item>
• Creates a MenuItem, which represents a single item in
a menu.
• It may contain a nested <menu>element in order to
create a submenu.
• <group>
• An optional, invisible container for <item> elements.
• It allows you to categorize menu items so they share
properties such as active state and visibility.
SLIDEBAZAAR |
⌐ You can add a submenu to an item The <item> element supports several attributes you can use to
in any menu (except a submenu) by define an item's appearance and behavior.
adding a <menu> element as the The items element has the following important attributes:
child of an <item>. ⌐ android:id :[- A resource ID that's unique to the item, which allows
⌐ Submenus are useful when your the application can recognize the item when the user selects it.
application has a lot of functions ⌐ android:icon :[- reference to a drawable to use as the item's icon.
that can be organized into topics, ⌐ android:title :[- A reference to a string to use as the item's title.
like items in a PC application's
⌐ android:showAsAction : [- Specifies when and how this item
menu bar (File, Edit, View, etc.).
should appear as an action item in the action bar.

SLIDEBAZAAR |
SLIDEBAZAAR |
 The options menu is where you should include actions and other options that are relevant
to the current activity context, such as "Search," "Compose email," and "Settings.“
 To specify the options menu for an activity, override onCreateOptionsMenu(). In this
method, you can inflate your menu resource (defined in XML) into the Menu provided in
the callback.

√ You can also add menu items using add() and retrieve items
with findItem() to revise their properties with MenuItem APIs.

SLIDEBAZAAR |
 When the user selects an item from the options menu (including action items in the
action bar), the system calls your activity's onOptionsItemSelected() method, which
passes the MenuItem selected.
 You can identify the item by calling getItemId(), which returns the unique ID for the
menu item (defined by the android:id attribute in the menu resource or with an
integer given to the add() method).
 You can match this ID against known menu items to perform the appropriate action.

 When you successfully handle


a menu item, return true.
 If you don't handle the menu
item, you should call the
superclass implementation
of onOptionsItemSelected()
(the default implementation
returns false).

SLIDEBAZAAR |
SLIDEBAZAAR |
 A contextual menu 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.

Screenshots of a floating context menu (left) and the contextual action bar (right).
SLIDEBAZAAR |
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 active, users can perform an action on multiple items at
once (if your app allows it).

To provide a floating context menu:


Register the View to which the context menu should be associated by
calling registerForContextMenu()and pass it the View. If your activity uses
a ListView or GridView and you want each item to provide the same context menu, register all
items for a context menu by passing the ListView or GridView to registerForContextMenu().
Implement the onCreateContextMenu() method in your Activity. When the registered view
receives a long-click event, the system calls your onCreateContextMenu()method. This is where
you define the menu items, usually by inflating a menu resource.

SLIDEBAZAAR |
• MenuInflater allows you to inflate the context menu from a menu resource. The callback
method parameters include the View that the user selected and a ContextMenu.
ContextMenuInfo object that provides additional information about the item selected.
• If your activity has several views that each provide a different context menu, you might use
these parameters to determine which context menu to inflate.

SLIDEBAZAAR |
 Implement onContextItemSelected( ):
 When the user selects a menu item, the system calls this method so you can perform
the appropriate action.

SLIDEBAZAAR |
SLIDEBAZAAR |
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 (such as
Gmail's email headers).
Providing a second part of a command sentence (such as a button marked "Add" that
produces a popup menu with different "Add" options).
Providing a drop-down similar to Spinner that does not retain a persistent selection.
If you define your menu in XML, here's how you can show the popup menu:
Instantiate a PopupMenu with its constructor, which takes the current
application Context and the View to which the menu should be anchored.
Use MenuInflater to inflate your menu resource into the Menu object returned
by using PopupMenu.inflate().
Call PopupMenu.show().

SLIDEBAZAAR |
SLIDEBAZAAR |
• To perform an action when the user selects a menu item, you must implement the
PopupMenu.OnMenuItemClickListener interface and register it with your PopupMenu by calling
setOnMenuItemclickListener().
• When the user selects an item, the system calls the onMenuItemClick() callback in your interface.

SLIDEBAZAAR |
SLIDEBAZAAR |
SLIDEBAZAAR |
• 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> element in
your menu resource or by specifying a group ID with the add() method.

SLIDEBAZAAR |
A menu can be useful as an interface for turning options on and off, using a checkbox for
stand-alone options, or radio buttons for groups of mutually exclusive options.
You can define the checkable behavior for individual menu items using the
android:checkable attribute in the <item> element, or for an entire group with
the android:checkableBehavior attribute in the <group> element.

SLIDEBAZAAR |
 The android:checkableBehavior attribute accepts either:
 Single: Only one item from the group can be checked (radio buttons)
 all: All items can be checked (checkboxes)
 none: No items are checkable
 You can apply a default checked state to an item using the android:checked attribute in
the <item> element and change it in code with the setChecked() method.
 When a checkable item is selected, the system calls your respective item-selected callback
method (such as onOptionsItemSelected()).
 You can query the current state of the item (as it was before the user selected it)
with isChecked() and then set the checked state with setChecked().

SLIDEBAZAAR |
SLIDEBAZAAR |
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.

SLIDEBAZAAR |
The Dialog class is the base class for dialogs, but you have to use one of the
following subclasses:
 AlertDialog: A dialog that can show a title, up to three buttons, a list of
selectable items, or a custom layout.
 DatePickerDialog or TimePickerDialog: A dialog with a pre-defined UI that
allows the user to select a date or time.
These classes define the style and structure for your dialog, but you should use
a DialogFragment as a container for your dialog. The DialogFragment class
provides all the controls you need to create your dialog and manage its appearance,
instead of calling methods on the Dialog object.
Using DialogFragment to manage the dialog ensures that it correctly handles
lifecycle events such as when the user presses the Back button or rotates the screen.
The DialogFragment class also allows you to reuse the dialog's UI as an embeddable
component in a larger UI.

SLIDEBAZAAR |
You can accomplish a wide variety of dialog designs by extending DialogFragment and creating
a AlertDialog in the onCreateDialog() callback method.

SLIDEBAZAAR |
The AlertDialog class allows you to build a variety of dialog
designs and is often the only dialog class you'll need. As
shown in figure, there are three regions of an alert dialog:
 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, you don't need a title.
 Content area: This can display a message, a list, or
other custom layout.
 Action buttons: There should be no more than three
action buttons in a dialog.

SLIDEBAZAAR |
To add action buttons, call the setPositiveButton() and setNegativeButton() methods:

SLIDEBAZAAR |
The set...Button() methods require a title for the button (supplied by a string resource) and a
DialogInterface.OnClickListener that defines the action to take when the user presses the
button.
There are three different action buttons you can add:
 Positive: You should use this to accept and continue with the action (the "OK" action).
 Negative: You should use this to cancel the action.
 Neutral: You should use this when the user may not want to proceed with the action,
but doesn't necessarily want to cancel. It appears between the positive and negative
buttons. For example, the action might be "Remind me later.“
You can add only one of each button type to an AlertDialog. That is, you cannot have more
than one "positive" button.

SLIDEBAZAAR |
There are three kinds of lists available with the AlertDialog APIs:
 A traditional single-choice list
 A persistent single-choice list (radio buttons)
 A persistent multiple-choice list (checkboxes)
To create a single-choice list like the one in figure, use the setItems() method:

SLIDEBAZAAR |
To add a list of multiple-choice items (checkboxes) or single-choice items
(radio buttons), use the
setMultiChoiceItems() or setSingleChoiceItems()methods, respectively.
For example, here's how you can create a multiple-choice list like the one
shown in figure that saves the selected items in an ArrayList:

SLIDEBAZAAR |
SLIDEBAZAAR |
Notifications Toasts
→Display Elements
→The Basics
→Creating Notifications
→Notification Actions →Positioning a Toast
→Managing Notifications
→Removing Notifications
→Displaying Progress

SLIDEBAZAAR |
Notifications
• A notification is a message you can display to the user outside of your
application's normal UI.
• When you tell the system to issue a notification, it first appears as an icon
in the notification area.
• To see the details of the notification, the user opens the notification drawer.
• Both the notification area and the notification drawer are system-controlled
areas that the user can view at any time.

SLIDEBAZAAR |
….cont

Figure 1. Notifications in the notification area.

Figure 2. Notifications in the notification drawer.

SLIDEBAZAAR |
Notification Display Elements
Notifications in the notification drawer can appear in one of two
visual styles, depending on the version and the state of the drawer:
• Normal view: The standard view of the notifications in the
notification drawer.
• Big view: A large view that's visible when the notification is
expanded. Big view is part of the expanded notification feature
available as of Android 4.1.

SLIDEBAZAAR |
Normal view
• A notification in normal view appears in an area that's up
to 64 dp tall.
• Even if you create a notification with a big view style, it The callouts in the illustration refer
will appear in normal view until it's expanded. to the following:
• An example of a normal view: 1.Content title
2.Large icon
3.Content text
4.Content info
5.Small icon
6.Time that the notification was
Figure 3. Notification in normal view. issued

SLIDEBAZAAR |
Big view
• A notification's big view appears only
when the notification is expanded,
which happens when the notification is
at the top of the notification drawer, or
when the user expands the notification
with a gesture.
• Expanded notifications are available
starting with Android 4.1.
• The following screenshot shows an
inbox-style notification: Figure 4. Big view notification.

SLIDEBAZAAR |
…cont
• Notice that the big view shares most of its visual elements with the normal view.
• The only difference is callout number 7, the details area. Each big view style sets this area in
a different way.
• The available styles are:
• Big picture style: The details area contains a bitmap up to 256 dp tall in its detail section.
• Big text style: Displays a large text block in the details section.
• Inbox style: Displays lines of text in the details section.
All of the big view styles also have the following content options that aren't available in
normal view:
• Big content title: Allows you to override the normal view's content title with a title that appears only in
the expanded view.
• Summary text: Allows you to add a line of text below the details area.

SLIDEBAZAAR |
Creating a Notification
The Notification.Builder provides an builder interface to create an Notification object

• Notification in android are represented by the Notification class.


• To create notification use NotificationManager class which can be received from the Context, eg.an activity or a service
,via GetSystemService() method.
• The Notification.Builder provides an builder interface to create an Notification object
• PendingIntent to specify the action which should be performed once the user select the notification.
Required notification contents
• A Notification object must contain the following:
• A small icon, set by setSmallIcon()
• A title, set by setContentTitle()
• Detail text, set by setContentText()

SLIDEBAZAAR |
....cont

SLIDEBAZAAR |
Managing Notifications
• When you need to issue a notification multiple times for the same type of event, you
should avoid making a completely new notification.
• Instead, you should consider updating a previous notification, either by changing
some of its values or by adding to it, or both.
• For example, Gmail notifies the user that new emails have arrived by increasing its
count of unread messages and by adding a summary of each email to the
notification.

SLIDEBAZAAR |
Removing notifications
• Notifications remain visible until one of the following happens:
• The user dismisses the notification either individually or by using "Clear All" (if the notification
can be cleared).
• The user clicks the notification, and you called setAutoCancel() when you created the
notification.
• You call cancel() for a specific notification ID. This method also deletes ongoing notifications.
• You call cancelAll(), which removes all of the notifications you previously issued.

SLIDEBAZAAR |
Toasts
• A toast provides simple feedback about an operation in a
small popup.
• It only fills the amount of space required for the message
and the current activity remains visible and interactive.
• Toasts automatically disappear after a timeout.
• For example, navigating away from an email before you
send it triggers a "Draft saved" toast to let you know that
you can continue editing later.

SLIDEBAZAAR |
The Basics
• First, instantiate a Toast object with one of the makeText() methods.
• This method takes three parameters: the application Context, the text message, and the duration
for the toast.
• It returns a properly initialized Toast object.
• You can display the toast notification with show(), as shown in the following
example:

SLIDEBAZAAR |
Positioning your Toast
• A standard toast notification appears near the bottom of the screen, centered horizontally.
• You can change this position with the setGravity(int, int, int) method.
• This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.
• For example, if you decide that the toast should appear in the top-left corner, you can set the gravity like this:

• If you want to nudge the position to the right, increase the value of the second parameter. To nudge it down, increase
the value of the last parameter.

SLIDEBAZAAR |
SLIDEBAZAAR |
Sending SMS Message
• SMS messaging is one of the main killer applications on a mobile phone today — for some users as necessary
as the phone itself. Any mobile phone you buy today should have at least SMS messaging capabilities, and
nearly all users of any age know how to send and receive messages. Android comes with a built-in SMS
application that enables you to send and receive SMS messages.
• However, in some cases you might want to integrate SMS capabilities into your own Android application.
For example, you might want to write an application that automatically sends a SMS message at regular
time intervals. For example, this would be useful if you wanted to track the location of your kids — simply
give them an Android device that sends out an SMS message containing its geographical location every 30
minutes. Now you know if they really went to the library after school! (Of course, that would also mean you
would have to pay the fees incurred in sending all those SMS messages…)

In AndroidManifest.XML code add:


<uses-permission android:name = ”android.permission.SEND_SMS”></uses-permission>

SLIDEBAZAAR |
Sending SMS Message Programmatically
public​ class​ MainActivity ​extends​ Activity​{
//---sends an SMS message to another device---​​private
​Button btnSendSMS;
public void sendSMS(String phoneNumber, String message) ​{
​@Override
​ SmsManager sms = SmsManager.getDefault();
​Public ​void ​onCreate(Bundle​ savedInstanceState)​{
​super.onCreate(savedInstanceState); ​ sms.sendTextMessage(phoneNumber, null, message, null, null);

setContentView(R.layout.main); ​ }

​btnSendSMS= (Button) findViewById(R.id.btnSendSMS);


​btnSendSMS.setOnClickListener(new View.OnClickListener()​​{
​public void onClick(View v)​​{
​sendSMS(“5556”, “Hello my friends!”);
​}
​});
​}

SLIDEBAZAAR |
How it works?
• Android uses a permissions-based policy whereby all the permissions needed by an application must be specified in the AndroidManifest.xml
file. This ensures that when the application is installed, the user knows exactly which access permissions it requires. Because sending SMS
messages incurs additional costs on the user’s end, indicating the SMS permissions in the AndroidManifest.xml file enables users to decide
whether to allow the application to install or not. To send an SMS message programmatically, you use the SmsManager class. Unlike other
classes, you do not directly instantiate this class; instead, you call the getDefault() static method to obtain a SmsManager object. You then
send the SMS message using the sendTextMessage() method:
Following are the five arguments to the sendTextMessage()method:
String SENT = “SMS_SENT”;
o destinationAddress Phone number of the recipient
​String DELIVERED = “SMS_DELIVERED”;
o scAddress — Service center address; use null for default SMSC
​PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,​​new Intent(SENT),0);
o text — Content of the SMS message
​PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, newIntent(DELIVERED), 0);
o sentIntent — Pending intent to invoke when the message is sent
o deliveryIntent — Pending intent to invoke when the message
has been delivered.
​SmsManagerObject. sendTextMessage(destinationAddress scAddress , text , sentIntent , deliveryIntent );

SLIDEBAZAAR |
SLIDEBAZAAR |
THANKYOU
SLIDEBAZAAR |

You might also like