Alert Dialog Tutorial With Example In Android Studio | Bkhakt Kavi Narsinh Mehta University Junagadh | BKNMU

 



Dialogs in android

 

AlertDialog is commonly used to prompt users with crucial information or confirmation before performing certain actions, such as exiting an app. Here's an example demonstrating how to create an AlertDialog to confirm exiting the app:

 

AlertDialog.Builder Components Used In Alert Dialog

 

AlertDialog.Builder is used to create an interface for Alert Dialog in Android for setting like alert title, message, image, button, button onclick functionality etc.

 

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

 

Methods of Alter Dialogs

setTitle(CharSequence title) – This component is used to set the title of the alert dialog. It is optional component.

setIcon(Drawable icon) – This component add icon before the title. You will need to save image in drawable icon.

 

setMessage(CharSequence message) – This component displays the required message in the alert dialog.

 

setCancelable(boolean cancelable) – This component has boolean value i.e true/false. If set to false it allows to cancel the dialog box by clicking on area outside the dialog else it allows.

 

setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener) – This component add positive button and further with this user confirm he wants the alert dialog question to happen.

 

alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface arg0, int arg1) {

finish();

}

});

 

setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener) – This component add negative button and further with this user confirm he doesn’t want the alert dialog question to happen.

 

alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this,"You clicked over No",Toast.LENGTH_SHORT).show();

}

});

 

setNeutralButton(CharSequence text, DialogInterface.OnClickListener listener) – This component simply add a new button and on this button developer can set any other onclick functionality like cancel button on alert dialog.

 

alertDialogBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Toast.makeText(getApplicationContext(),"You clicked on Cancel",Toast.LENGTH_SHORT).show();

}

});

 

 Youtube Example click here to watch BCA SCHOOL

 

Example:

1.Create a new project and name it AlertDialogExample.

2.in activity_main.xml

3.take one button and one text view.

4.Mainactivity.java(below code).


alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface arg0, int arg1) {

finish();

}

});

 

setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener) – This component add negative button and further with this user confirm he doesn’t want the alert dialog question to happen.

 

alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this,"You clicked over No",Toast.LENGTH_SHORT).show();

}

});

 

setNeutralButton(CharSequence text, DialogInterface.OnClickListener listener) – This component simply add a new button and on this button developer can set any other onclick functionality like cancel button on alert dialog.

 

alertDialogBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Toast.makeText(getApplicationContext(),"You clicked on Cancel",Toast.LENGTH_SHORT).show();

}

});



#android #androidapps #androidgames #androiddevelopment #androidwear #androidstudio #androidroot #androidsecurity #androidtips #androidnews #androidcommunity #androidlovers #androidphotography #androidart #androidwallpapers #androidthemes #androidcustomization #androidmod #androidrooting




Post a Comment

Thanks for comment.

Previous Post Next Post