Dialog Fragment
Dialog Fragment
▪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);