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.
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.
- Single Button
- Two Buttons
- 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
In Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
In Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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
In Kotlin
This result of above code will be like following image.
Showing toast function:
In Java
In Kotlin
This output of above code will display like following image
In Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
In Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
In Kotlin
This result of above code will be like following image.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void showToastMessage(CharSequence msg) { | |
Snackbar snackBar = | |
Snackbar.make(getActivity().findViewById(android.R.id.content), msg, Snackbar.LENGTH_LONG); | |
snackBar.show(); | |
} |
In Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Dialogs | How to Show Alert Dialog in Android
Reviewed by Unknown
on
February 14, 2018
Rating:

No comments: