You are on page 1of 13

Mobile Application Development in

Android

Bangladesh-Korea Information Access Center (IAC)


Department of Computer Science and Engineering (CSE)
Bangladesh University of Engineering and Technology (BUET)
ANDROID NOTIFICATION
Notification

• Notifies a user about some events


• Some events require the user to respond and others
do not.
• Some situations
– When an event such as saving a file is complete, a small
message should appear to confirm that the save was successful.
– If the application is running in the background and needs the
user's attention, the application should create a notification that
allows the user to respond at his or her convenience.
– If the application is performing work that the user must wait for
(such as loading a file), the application should show a hovering
progress wheel or bar.
Types of Notification

• Toast Notification
Types • Status Notification
• Dialog Notification
Toast Notification

• A toast notification is a
message that pops up on
the surface of the window.
– It only fills the amount of space
required for the message
– The user's current activity
remains visible and interactive.
– The notification automatically
fades in and out,
– Does not accept interaction
events.
– Because a toast can be created
from a background Service
– It appears even if the application
isn't visible.
Toast Implementation (Explanation)

• We will use makeText static method of Toast


class to show a toast
• makeText function has two syntax
– public static Toast makeText (Context context, int resId,
int duration)
– public static Toast makeText (Context context,
CharSequence text, int duration
• The common parameters
– context :Usually your Application or Activity object. We can
have a reference to current application context using
getApplicationContext() method
– duration : How long to display the message. Either
LENGTH_SHORT or LENGTH_LONG
Toast Implementation (Explanation)

• The other parameter


– It is either resId or text. This specifies the formatted text to be
displayed on the Toast
– resId: The resource id of the string resource to use
– text: The text to show
Toast Implementation (Syntax)

Toast.makeText(getApplicationContext(), "Hello toast!";


Toast.LENGTH_SHORT). show();
Toast Implementation with Positioning
(Explanation)
• With using the setGravity method of the Toast
– public void setGravity (int gravity, int xOffset, int yOffset)
• Set the location at which the notification should
appear on the screen.
• Remember! It’s not a static method!
Toast Implementation with Positioning
(Explanation)
• Parameters
– gravity specifies placement of an object within a potentially
larger container.
– We will use the static constants defined in Gravity class of
Android
– We can use more than one constants by placing bitwise or sign
(|) between two. In that case the object will get the resultant
gravity.
Toast Implementation with Positioning
(Explanation)
– Most usable gravity constants
• BOTTOM
• CENTER
• CENTER_HORIZONTAL
• CENTER_VERTICAL
• FILL
• FILL_HORIZONTAL
• FILL_VERTICAL
• LEFT
• NO_GRAVITY
• RIGHT
• TOP
Toast Implementation with Positioning
(Syntax)
Toast toast = Toast.makeText(getApplicationContext(),
R.string.hello, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.RIGHT, 100, 50);
toast.show();
Status and Dialog
Notification Later

You might also like