0
0
Android-kotlinHow-ToBeginner ยท 3 min read

How to Create Dialog in Android: Simple Guide with Example

To create a dialog in Android, use the AlertDialog.Builder class to build and show a dialog window. You set the title, message, and buttons, then call show() to display it.
๐Ÿ“

Syntax

The basic syntax to create a dialog uses AlertDialog.Builder. You create a builder, set properties like title and message, add buttons with listeners, then call show() to display.

  • Context: The current activity or context.
  • setTitle: Sets the dialog title.
  • setMessage: Sets the dialog message.
  • setPositiveButton: Adds a button with a click listener.
  • show: Displays the dialog.
java
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("OK", (dialog, which) -> {
    // Handle button click
});
builder.show();
Output
A dialog window appears with the title 'Title', message 'Message', and an OK button.
๐Ÿ’ป

Example

This example shows a simple dialog with a title, message, and OK button that closes the dialog when clicked.

java
import android.app.AlertDialog;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Welcome");
        builder.setMessage("This is a simple dialog.");
        builder.setPositiveButton("OK", (dialog, which) -> dialog.dismiss());
        builder.show();
    }
}
Output
When the app starts, a dialog appears with title 'Welcome', message 'This is a simple dialog.', and an OK button that closes the dialog.
โš ๏ธ

Common Pitfalls

  • Forgetting to call show() means the dialog will not appear.
  • Using the wrong context (like application context) can cause a crash; always use an activity context.
  • Not handling button clicks can leave the dialog open or unresponsive.
  • Creating dialogs on background threads causes errors; always create dialogs on the main UI thread.
java
/* Wrong: Missing show() - dialog won't appear */
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Oops");
builder.setMessage("No show called");

/* Right: Call show() to display */
builder.show();
Output
Without show(), no dialog appears; with show(), the dialog is visible.
๐Ÿ“Š

Quick Reference

Use AlertDialog.Builder to create dialogs with these common methods:

  • setTitle(String): Set dialog title
  • setMessage(String): Set dialog message
  • setPositiveButton(String, Listener): Add positive button
  • setNegativeButton(String, Listener): Add negative button
  • setCancelable(boolean): Allow dismiss on outside touch
  • show(): Display the dialog
โœ…

Key Takeaways

Use AlertDialog.Builder with an activity context to create dialogs.
Always call show() to display the dialog.
Set title, message, and buttons to customize the dialog.
Handle button clicks to respond to user actions.
Create dialogs on the main UI thread to avoid crashes.