Docsity

Android Alert Dialog, Exams of Computer Science

Toggle sidebar
bg1
PreviousPage NextPage
AndroidAlertDialog
Advertisements
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=newAlertDialog.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
setOnCancelListe ner(DialogInterface.OnCancelListeneronCancelList ener)
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
Aftercreatingandsettingthedialogbuilder,youwillcreateanalertdialogbycallingthecreate()methodofthebuilderclass.Itssyntaxis
AlertDialogalertDialog=alertDialogBuilder.create();
alertDialog.show();
Thiswillcreatethealertdialogandwillshowitonthescreen.

Partial preview of the text

Download Android Alert Dialog and more Exams Computer Science in PDF only on Docsity!

Previous Page Next Page

Android Alert Dialog

Advertisements

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

setIcon(Drawable icon)

This method set the icon of the alert dialog box.

setCancelable(boolean cancel able)

This method sets the property that the dialog can be cancelled or not

setMessage(CharSequence message)

This method sets the message to be displayed in the alert dialog

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

setOnCancelListener(DialogInterface.OnCancelListener onCancelListener)

This method Sets the callback that will be called if the dialog is cancelled.

setTitle(CharSequence title)

This method set the title to be appear in the dialog

After creating and setting the dialog builder , you will create an alert dialog by calling the create() method of the builder class. Its syntax is

AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show();

This will create the alert dialog and will show it on the screen.

 (^) 