Dialogs | How to Show Alert Dialog in Android

Dialogs | Definition 

A dialog is a small window that prompts the user to make a decision or enter additional information. 

The full source code is available here.

Creating AlertDialog is very simple. In this tutorial I'll be discussing about creating Alert Dialogs with the following categories.
  1. Single Button
  2. Two Buttons
  3. Three Buttons

1. Single Button - Alert Dialog

         The following code will be create a simple alert dialog with one button. Sometimes your application is need to indicate such as the process is completed, phone contact list is loaded, etc

In Java


AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
//set title
alertDialog.setTitle("Alert Dialog Demo");
//set message
alertDialog.setMessage(getResources().getString(R.string.title));
//set icon
alertDialog.setIcon(R.drawable.ic_done_black_24dp);
//set outside focus
alertDialog.setCancelable(false);
//set button
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
showToastMessage("You tapped OK");
}
});
//show alert dialog
alertDialog.show();
view raw JavaFragment.kt hosted with ❤ by GitHub

     

In Kotlin

//build alert dialog
val alertDialog = AlertDialog.Builder(activity)
//set title
alertDialog.setTitle("Alert Dialog Demo")
//set message
alertDialog.setMessage(resources.getString(R.string.title))
//set icon
alertDialog.setIcon(R.drawable.ic_done_black_24dp)
//set button
alertDialog.setPositiveButton("OK") { dialog, which ->
showToastMessage("You tapped OK")
}
//show alert dialog
alertDialog.show()

This result of above code will be like following image.



2. Two Buttons - Alert Dialog


         The following code will be create a simple alert dialog with two buttons. Sometimes your application is need to ask such as remove file from the database or the file manager, etc



In Java

AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
//set title
alertDialog.setTitle("Confirm Remove...");
//set description
alertDialog.setMessage("Are you sure you want to remove this?");
//set icon
alertDialog.setIcon(R.drawable.ic_clear_black_24dp);
//set positive button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
showToastMessage("You tapped on YES");
}
});
//set negative button
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
showToastMessage("You tapped on NO");
dialog.cancel();
}
});
//show dialog
alertDialog.show();
view raw JavaFragment.kt hosted with ❤ by GitHub

In Kotlin

val alertDialog = AlertDialog.Builder(activity)
alertDialog.setTitle("Confirm Remove...")
alertDialog.setMessage("Are you sure you want to remove this?")
alertDialog.setIcon(R.drawable.ic_clear_black_24dp)
alertDialog.setPositiveButton("YES"
) { dialog, which ->
showToastMessage("You tapped on YES")
}
alertDialog.setNegativeButton("NO"
) { dialog, which ->
showToastMessage("You tapped on NO")
dialog.cancel()
}
alertDialog.show()






This result of above code will be like following image.



3. Three Buttons - Alert Dialog


         The following code will be create a simple alert dialog with three buttons. Sometimes your application is need to ask save some file to the database.


In Java

AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setTitle("Save File...");
alertDialog.setMessage("Do you want to save this file?");
alertDialog.setIcon(R.drawable.ic_save_black_24dp);
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
showToastMessage("You tapped on YES");
}
});
alertDialog.setNeutralButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
showToastMessage("You tapped on NO");
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
showToastMessage("You tapped on Cancel");
}
});
alertDialog.show();
view raw JavaFragment.kt hosted with ❤ by GitHub




In Kotlin

val alertDialog = AlertDialog.Builder(activity)
alertDialog.setTitle("Save File...")
alertDialog.setMessage("Do you want to save this file?")
alertDialog.setIcon(R.drawable.ic_save_black_24dp)
alertDialog.setPositiveButton("YES"
) { dialog, which ->
showToastMessage("You tapped on YES")
}
alertDialog.setNeutralButton("NO"
) { dialog, which ->
showToastMessage("You tapped on NO")
}
alertDialog.setNegativeButton("Cancel"
) { dialog, which ->
showToastMessage("You tapped on Cancel")
}
alertDialog.show()

This result of above code will be like following image.


Showing toast function:

In Java

private void showToastMessage(CharSequence msg) {
Snackbar snackBar =
Snackbar.make(getActivity().findViewById(android.R.id.content), msg, Snackbar.LENGTH_LONG);
snackBar.show();
}
view raw JavaFragment.kt hosted with ❤ by GitHub



In Kotlin

private fun showToastMessage(msg: CharSequence) {
val snackBar = activity.findViewById<View>(android.R.id.content)?.let {
Snackbar.make(it, msg, Snackbar.LENGTH_LONG)
}
snackBar?.show()
}



This output of above code will display like following image



The full source code is available here.

Dialogs | How to Show Alert Dialog in Android Dialogs |  How to Show Alert Dialog in Android Reviewed by Unknown on February 14, 2018 Rating: 5

No comments:

Powered by Blogger.