How to Implement Five Common Android Dialog Types

This tutorial walks through creating five typical Android dialogs—normal, list‑based, custom‑view, progress bar, and date picker—by adding buttons to a layout, writing click handlers, and using AlertDialog, ProgressDialog, and DatePickerDialog with complete code examples and screenshots.

The Dominant Programmer
The Dominant Programmer
The Dominant Programmer
How to Implement Five Common Android Dialog Types

Scenario

The article introduces five commonly used dialog types in Android: normal dialog, list dialog, custom view dialog, progress bar dialog, and date picker dialog.

Normal Dialog

Add a <Button> to activity_main.xml with android:onClick="startNormalDialog" and label it "Launch Normal Dialog".

<Button
    android:id="@+id/button_normal_dialog"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="startNormalDialog"
    android:text="Launch Normal Dialog" />

Implement the click method in the activity:

public void startNormalDialog(View view){
    new AlertDialog.Builder(this)
        .setIcon(R.drawable.ic_launcher_background)
        .setTitle("Public Account: Dominant Programmer")
        .setCancelable(false)
        .setMessage("Delete this record?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"Record deleted successfully",Toast.LENGTH_LONG).show();
            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"Deletion cancelled",Toast.LENGTH_LONG).show();
            }
        })
        .show();
}

Run the project to see the dialog.

Normal dialog screenshot
Normal dialog screenshot

List Dialog

Add another button with android:onClick="startListDialog" and label "Launch List Dialog".

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="startListDialog"
    android:text="Launch List Dialog" />

Implement the click method:

public void startListDialog(View view){
    final String[] items = {"Follow","Public Account","Dominant Programmer","Get","Programming tutorials and resources"};
    new AlertDialog.Builder(this)
        .setTitle("Public Account: Dominant Programmer")
        .setCancelable(false)
        .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_LONG).show();
            }
        })
        .setPositiveButton("OK", null)
        .show();
}

Run to view the list dialog.

List dialog screenshot
List dialog screenshot

Custom View Dialog

Create a layout file dialog_view.xml containing two EditText fields for username and password.

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <EditText
        android:id="@+id/text_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Username"
        android:inputType="textPersonName" />
    <EditText
        android:id="@+id/text_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:inputType="textPassword" />
</androidx.appcompat.widget.LinearLayoutCompat>

Add a button labeled "Launch Custom Dialog" to the main layout.

<Button
    android:id="@+id/button3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="startCustomDialog"
    android:text="Launch Custom Dialog" />

Implement the click method to inflate the custom view and retrieve input values:

public void startCustomDialog(View view){
    View v = View.inflate(this, R.layout.dialog_view, null);
    final EditText user = v.findViewById(R.id.text_username);
    final EditText pwd = v.findViewById(R.id.text_pwd);
    new AlertDialog.Builder(this)
        .setTitle("Public Account: Dominant Programmer")
        .setCancelable(false)
        .setView(v)
        .setPositiveButton("Login", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String username = user.getText().toString().trim();
                String password = pwd.getText().toString().trim();
                Toast.makeText(MainActivity.this,"Username:"+username+" Password:"+password,Toast.LENGTH_LONG).show();
            }
        })
        .show();
}

Run to see the custom view dialog.

Custom view dialog screenshot
Custom view dialog screenshot

Progress Bar Dialog

Add a button labeled "Launch Progress Dialog".

<Button
    android:id="@+id/button4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="startProgressDialog"
    android:text="Launch Progress Dialog" />

Implement the click method to show a horizontal progress dialog that updates in a background thread:

public void startProgressDialog(View view){
    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setTitle("Public Account: Dominant Programmer");
    progressDialog.setCancelable(false);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.show();
    new Thread(new Runnable() {
        @Override
        public void run() {
            for(int i=1;i<=20;i++){
                try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); }
                progressDialog.setProgress(progressDialog.getProgress()+5);
            }
            progressDialog.dismiss();
        }
    }).start();
}

Run to view the progress dialog.

Progress dialog screenshot
Progress dialog screenshot

Date Picker Dialog

Add a button labeled "Launch Date Picker Dialog".

<Button
    android:id="@+id/button5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="startDatePickDialog"
    android:text="Launch Date Picker Dialog" />

Implement the click method to display a date picker and show the selected date in a toast:

public void startDatePickDialog(View view){
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            Toast.makeText(MainActivity.this,"Selected "+year+" year "+month+" month "+dayOfMonth+" day",Toast.LENGTH_LONG).show();
        }
    }, year, month, day).show();
}

Run to see the date picker dialog.

Date picker dialog screenshot
Date picker dialog screenshot
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AndroidMobile UIDialogCustomViewAlertDialogDatePickerDialogProgressDialog
The Dominant Programmer
Written by

The Dominant Programmer

Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.