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.
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.
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.
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.
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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
