PreviousPage NextPage
AndroidAlertDialog
Advertisements
ADialogissmallwindowthatpromptstheusertoadecisionorenteradditionalinformation.
Sometimesinyourapplication,ifyouwantedtoasktheuserabouttakingadecisionbetweenyesornoinresponseofanyparticularaction
takenbytheuser,byremaininginthesameactivityandwithoutchangingthescreen,youcanuseAlertDialog.
Inorderto makean alertdialog,you needto makean objectof AlertDialogBuilderwhichan innerclass ofAlertDialog.Its syntaxis given
below
AlertDialog.BuilderalertDialogBuilder=newAlertDialog.Builder(this);
Nowyouhavetosetthepositive(yes)ornegative(no)buttonusingtheobjectoftheAlertDialogBuilderclass.Itssyntaxis
alertDialogBuilder.setPositiveButton(CharSequencetext,
DialogInterface.OnClickListenerlistener)
alertDialogBuilder.setNegativeButton(CharSequencetext,
DialogInterface.OnClickListenerlistener)
Apartfromthis,youcanuseotherfunctionsprovidedbythebuilderclasstocustomizethealertdialog.Thesearelistedbelow
Sr.No Methodtype&description
1
setIcon(Drawableicon)
Thismethodsettheiconofthealertdialogbox.
2
setCancelable(booleancancelable)
Thismethodsetsthepropertythatthedialogcanbecancelledornot
3
setMessage(CharSequencemessage)
Thismethodsetsthemessagetobedisplayedinthealertdialog
4
setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener
listener)
Thismethodsetslistofitemstobedisplayedinthedialogasthecontent.Theselectedoptionwillbenotifiedbythelistener
5
setOnCancelListe ner(DialogInterface.OnCancelListeneronCancelList ener)
ThismethodSetsthecallbackthatwillbecalledifthedialogiscancelled.
6
setTitle(CharSequencetitle)
Thismethodsetthetitletobeappearinthedialog
Aftercreatingandsettingthedialogbuilder,youwillcreateanalertdialogbycallingthecreate()methodofthebuilderclass.Itssyntaxis
AlertDialogalertDialog=alertDialogBuilder.create();
alertDialog.show();
Thiswillcreatethealertdialogandwillshowitonthescreen.