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. Working with Dialog Boxes
The Android Dialog class (android.app.Dialog) is the base class for all types of dialog controls that we can use within our Activity classes. Dialogs exists within the lifecycle of our Activity (android.app.Activity). They pop up in the foreground, blocking of our Activity screen, to catch the user’s attention for a variety of reasons.
The Purpose of Dialogs :
Inform the user of some event or progress (e.g. “You have mail!” Or “Downloading Message 1 of 200,000”)
Force the user to confirm an action (e.g. “Are you sure you want to delete all your contacts? Really sure?”)
Prompt the user for further information and collect it (e.g. “Please enter your username and password.”)
A Note on Toast Messages: Some developers also use Toast messages (android.widget.Toast) for sending simple notifications or messages to the user. A Toast message displays over our Activity screen for a few seconds and then disappears automatically.
Toast over a Dialog is as follows: if the user is being informed of non-essential information, use a Toast, but if information is essential, use a Dialog. We use Toasts as very lightweight, informational notifications.
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. 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. Toasts automatically disappear after a timeout
To instantiate a Toast object with one of the makeText() methods. This method takes three parameters:
- The application Context
- The text message
- 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:
Context context = getApplicationContext(); CharSequence text = "Hello toast!"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show();
This example demonstrates everything you need for most toast notifications. You should rarely need anything else. You may, however, want to position the toast differently or even use your own layout instead of a simple text message. The following sections describe how you can do these things.
You can also chain your methods and avoid holding on to the Toast object, like this:
Toast.makeText(context, text, duration).show();
Positioning of Toast
A standard toast notification appears near the bottom of the screen, cantered 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:
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
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.