Alert Dialog in Android Studio (Java)
Show an alert dialog that asks the user to confirm something, with Yes and No buttons that do different things.
What you'll learn
- Build a dialog with AlertDialog.Builder
- Add title, message and buttons
- Respond to Yes and No
Before you start
- Android Studio (any recent version)
- An emulator or an Android phone
How it fits together
- 1Button tap
- 2MaterialAlertDialogBuilder
- 3User picks a button
- 4Your code reacts
Build it step by step
1. Create the project
In Android Studio choose New Project → Empty Views Activity, with Language Java and Minimum SDK API 24.
2. Add two buttons
In activity_main.xml, add Delete a file and Choose a colour, plus a TextView that shows what the user picked.
3. A Yes/No dialog
MaterialAlertDialogBuilder builds a Material 3 dialog step by step: setTitle, setMessage, setIcon, then the buttons. Each button gets a small block of code that runs when it's tapped. Call show() at the end.
4. A list to choose from
setSingleChoiceItems shows radio buttons. Remember the tapped item, and apply it when the user taps OK.
5. Ask before leaving
Add an OnBackPressedCallback. When the user presses Back, show an "Leave the app?" dialog; Exit calls finish().
6. Run it
Try each button, then press Back.
Tips
- Put dialog text in
strings.xml, so it's easy to change or translate. - Use dialogs only for decisions that matter; too many annoy users.
What's improved over the video
Material 3 dialogs, a single-choice list, an exit confirmation using the current Back API, and all text in strings.xml.
Files affected
Check what to add and what to replace before copying code.
Application code
Select a file to see its code and where it belongs.
This code uses package com.example.alertdialog. Change that first line to your own package, the one at the top of your MainActivity.
app/src/main/java/com/example/alertdialog/MainActivity.javaShows a confirm dialog, a single-choice list, and an exit dialog on Back.
package com.example.alertdialog;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.activity.OnBackPressedCallback;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
/**
* Three common dialogs: a Yes/No confirmation, a single-choice list, and "Exit the app?" on Back.
* MaterialAlertDialogBuilder gives them the Material 3 look.
*/
public class MainActivity extends AppCompatActivity {
private static final String[] COLOURS = {"Purple", "Coral", "Sky blue", "Mint"};
private int chosenColour = 0;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Screens.setUp(this, R.layout.activity_main);
result = findViewById(R.id.textResult);
Button confirm = findViewById(R.id.btnConfirm);
confirm.setOnClickListener(v -> new MaterialAlertDialogBuilder(this)
.setTitle(R.string.delete_title)
.setMessage(R.string.delete_message)
.setIcon(R.drawable.ic_warning)
.setNegativeButton(R.string.cancel, (dialog, which) -> result.setText(R.string.result_kept))
.setPositiveButton(R.string.delete, (dialog, which) -> result.setText(R.string.result_deleted))
.show());
Button choose = findViewById(R.id.btnChoose);
choose.setOnClickListener(v -> new MaterialAlertDialogBuilder(this)
.setTitle(R.string.choose_title)
// The radio list remembers the current choice; OK confirms it.
.setSingleChoiceItems(COLOURS, chosenColour, (dialog, which) -> chosenColour = which)
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.ok, (dialog, which) -> result.setText(getString(R.string.result_colour, COLOURS[chosenColour])))
.show());
// Ask before leaving the app with the Back button.
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
new MaterialAlertDialogBuilder(MainActivity.this)
.setTitle(R.string.exit_title)
.setMessage(R.string.exit_message)
.setCancelable(true)
.setNegativeButton(R.string.stay, null)
.setPositiveButton(R.string.exit, (dialog, which) -> finish())
.show();
}
});
}
}


