A Dialog is small window that prompts the user to a decision or enter additional information.
Some times in your application, if you wanted to ask the user about taking a decision between yes or no in response of any particular action taken by the user, by remaining in the same activity and without changing the screen, you can use Alert Dialog.
In order to make an alert dialog, you need to make an object of AlertDialogBuilder which an inner class of AlertDialog. Its syntax is given below
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
Now you have to set the positive (yes) or negative (no) button using the object of the AlertDialogBuilder class. Its syntax is
alertDialogBuilder.setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener) alertDialogBuilder.setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener)
Apart from this , you can use other functions provided by the builder class to customize the alert dialog. These are listed below
Sr.No Method type & description
1
setIcon(Drawable icon)
This method set the icon of the alert dialog box.
2
setCancelable(boolean cancel able)
This method sets the property that the dialog can be cancelled or not
3
setMessage(CharSequence message)
This method sets the message to be displayed in the alert dialog
4
setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)
This method sets list of items to be displayed in the dialog as the content. The selected option will be notified by the listener
5
setOnCancelListener(DialogInterface.OnCancelListener onCancelListener)
This method Sets the callback that will be called if the dialog is cancelled.
6
setTitle(CharSequence title)
This method set the title to be appear in the dialog
Comments
Post a Comment