Using dialogs in android
▪A dialog is a smaller window that pops up in front of the current
window to show an urgent message, to prompt the user for a piece of
input, or to show some sort of status like the progress of a download.
▪ The user is generally expected to interact with the dialog and then
return to the window underneath to continue with the application
▪Dialogs that are explicitly supported in Android include the alert,
prompt, pick-list, single-choice, multiple-choice, progress, time-picker,
and date-picker dialogs.
Using dialogs in android
▪Dialogs in Android are asynchronous, which provides flexibility.
▪ With a synchronous dialog, the line of code after the dialog is shown
does not run until the dialog has been dismissed.
▪In Android ,As soon as the dialog has been shown, the next line of
code runs, even though the user hasn’t touched the dialog yet.
▪Callbacks from the dialog allow the application to be notified of user
interaction with the dialog
Dialog Fragment
▪DialogFragment is a utility class which extends the Fragment class.
▪ DialogFragment displays or shows a Dialog but inside a Fragment
▪i.e. all the information regarding the Dialog or the data associated
with the Dialog will be stored or managed in the Fragment only
▪Class must extend DialogFragment with at least onCreateDialog
and/or onCreateView implemented.
▪Dialogs can be created using DialogFragment in two ways:
▪onCreateDialog – Here you can create the AlertDialog using the
AlertDialog.Builder class.
▪onCreateView – Here you can create a Dialog using a custom view
defined.
For example, a basic AlertDialog that's managed within a DialogFragment
public class FireMissilesDialogFragment extends DialogFragment
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
// FIRE ZE MISSILES!
} })
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog } });
// Create the AlertDialog object and return it
return builder.create();
}}
Building an Alert Dialog
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.
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.
Building an Alert Dialog
▪To add action buttons , call the setPositiveButton() and
setNegativeButton() methods:
▪These 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
▪Positive :use this to accept and continue with the action (the "OK" action).
▪Negative:use this to cancel the action.
▪Neutral:use this when the user may not want to proceed with the action, but
doesn't necessarily want to cancel. 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.
Adding A List
▪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)
Adding A List
▪To create a single-choice list
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_color)
.setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
return builder.create();
}
▪To add a list of multiple-choice items (checkboxes) or single-choice items
(radio buttons), use the setMultiChoiceItems() or setSingleChoiceItems()
methods, respectively.
Working with Toast
▪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, clicking Send on an email triggers a "Sending
message...“
▪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
▪Constants of Toast class
▪There are only 2 constants of Toast class which are given below.
▪ public static final int LENGTH_LONG: displays view for the long duration of
time.
▪ public static final int LENGTH_SHORT: displays view for the short duration of
time
Methods of Toast class
▪The widely used methods of Toast class are given below.
▪ public static Toast makeText(Context context, CharSequence text, int
duration) :makes the toast containing text and duration.
▪ public void show():displays toast.
▪ public void setMargin (float horizontalMargin, float verticalMargin):changes
the horizontal and vertical margin difference.
▪ 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();
Or use this single line code
Toast.makeText(context, text, duration).show();
◦ Toast.makeText(getApplicationContext(),"Hello world",Toast.LENGTH_SHORT).show();
Toast
▪A standard toast notification appears near the bottom of the screen, centered horizontally.
▪This position can be changed with the setGravity(int, int, int) method.
▪ This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.
▪ If the toast should appear in the top-left corner, set the gravity like this
▪toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);