How to Create Alert Dialog in Android

Total Views : 454
Zoom In Zoom Out Read Later Print

In this tutorial we are creating dialogs in android. you can customize dialog as you want using custom layout.

We are creating 7 type of dialog in this tutorial. first one is using only two button and other is using some other view in dialog.

For this tutorial i am gonna show you method for these 7 dialogs, you can call for showing them. No extra code this time. But if you want you can download project.

1. Creating Simple Dialog

This method will create a simple dialog with two buttons.

Simple Dialog

void showSimple(){
AlertDialog alert = new AlertDialog.Builder(this).create();
alert.setTitle("ProDevs");
alert.setMessage("https://prodevsblog.com/");
alert.setButton(Dialog.BUTTON_POSITIVE,"OK",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
alert.setButton(Dialog.BUTTON_NEGATIVE,"CANCEL",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on CANCEL", Toast.LENGTH_SHORT).show();
}
});
alert.show();
}

2. Creating with List

Dialog with a list and single selection.

List Dialog

public void showListItems() {
final String[] items = {"Apple", "Samsung", "Vivo", "Google"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Provider")
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), items[which] + " is clicked", Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("OK", null);
builder.setNegativeButton("CANCEL", null);
builder.setNeutralButton("AUTO", null);
builder.setPositiveButtonIcon(getResources().getDrawable(android.R.drawable.ic_menu_call, getTheme()));
builder.setIcon(getResources().getDrawable(R.drawable.icons, getTheme()));

AlertDialog alertDialog = builder.create();
alertDialog.show();
}

3. Multi Select List Dialog

Dialog to get multiple input from user using list.

Multi Select Dialog

public void showMultiSelection() {
final String[] items = {"Apple", "Samsung", "Vivo", "Google"};
final ArrayList<Integer> selectedList = new ArrayList<>();
AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Select Provider");
builder.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
selectedList.add(which);
} else if (selectedList.contains(which)) {
selectedList.remove(which);
}
}
});
builder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ArrayList<String> selectedStrings = new ArrayList<>();
for (int j = 0; j < selectedList.size(); j++) {
selectedStrings.add(items[selectedList.get(j)]);
}
Toast.makeText(getApplicationContext(), "Selected Items: " + Arrays.toString(selectedStrings.toArray()), Toast.LENGTH_SHORT).show();
}
});
builder.show();
}

4. Getting Input Using EditText in Dialog

You can use EditText wit dialog to get input from user.

EditText & Dialog

public void showForInput() {

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter You Name");

final EditText input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), "Name : " + input.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
builder.show();
}

6. SeekBar with Dialog

Show SeekBar with Dialog to get some input.

SeekBar with Dialog

public void getProgress() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("SeekBar");
final SeekBar seekBar = new SeekBar(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
seekBar.setLayoutParams(lp);
builder.setView(seekBar);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), "Progress : " + seekBar.getProgress(), Toast.LENGTH_SHORT).show();
}
});
builder.show();
}

6. RatingBar with Dialog

Show RatingBar with Dialog to get some input.

dialog_rating.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RatingBar
android:id="@+id/ratingBar"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="0.5" />
</LinearLayout>

showRating()

public void showRating() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
builder.setTitle("RatingBar");
View dialogLayout = inflater.inflate(R.layout.dialog_rating, null);
final RatingBar ratingBar = dialogLayout.findViewById(R.id.ratingBar);
builder.setView(dialogLayout);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), "Rating : " + ratingBar.getRating(), Toast.LENGTH_SHORT).show();
}
});
builder.show();
}

7. Image and Dialog

Showing Image in Dialog.

dialog_image.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="@mipmap/ic_launcher"
android:layout_centerInParent="true"/>

</RelativeLayout>

showImage()

public void showImage() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.dialog_image, null);
builder.setPositiveButton("OK", null);
builder.setView(dialogLayout);
builder.show();
}

If you have any problem with above code you can comment or simply download our working code for this project.

See More

Latest Photos