Note: The Dialog class is the base class for dialog boxes, but you should avoid instantiating Dialog directly. Instead, use one of the following subclasses:
AlertDialog , DatePickerDialog or #
Recommendation Google itself in documentation of Android .
Context
Interface that contains global information about an application environment, is an abstract class whose implementation is provided by the Android System , it allows access to application-specific features as well as calls to application-level operations such as launch activities, transmission and receipt intentions, etc.
Some Objects and Components need a Context , you need to inform them what is going on.
You can use the method getActivity()
final Dialog dialogMensagem = new Dialog(getActivity());
AlertDialog
To use the AlertDialog class you will always do the AlertDialog.Builder class instance, but why? The AlertDialog class does not by default allow classes from different packages to instantiate it, but it gives us the built-in static class that gets a Context as parameter and that returns the object itself, that is, we can call more than one method at a time. So if you try:
AlertDialog dialogo = new AlertDialog();
Will return a compilation error. Instead do:
AlertDialog.Builder dialogo = new AlertDialog.Builder(this);
// Utiliza o método setTitle() para definir um titulo
dialogo.setTitle("Meu diálogo");
// Utiliza o método setMessage() para definir uma mensagem
dialogo.setMessage("Mensagem do diálogo.");
// Para exibir, use o método show()
dialogo.show();
Or you can do all this in just one had:
new AlertDialog.Builder(this).setTitle("Meu diálogo").setMessage("Mensagem do diálogo.").show();
See documentation to learn more about the class AlertDialog.Builder