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.
Shows a confirm dialog, a single-choice list, and an exit dialog on Back.
MainActivity.java
packagecom.example.alertdialog;importandroid.os.Bundle;importandroid.widget.Button;importandroid.widget.TextView;importandroidx.activity.OnBackPressedCallback;importandroidx.appcompat.app.AppCompatActivity;importcom.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.
*/publicclassMainActivityextendsAppCompatActivity{privatestaticfinalString[]COLOURS={"Purple","Coral","Sky blue","Mint"};privateint chosenColour =0;TextView result;@OverrideprotectedvoidonCreate(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 ->newMaterialAlertDialogBuilder(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 ->newMaterialAlertDialogBuilder(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,newOnBackPressedCallback(true){@OverridepublicvoidhandleOnBackPressed(){newMaterialAlertDialogBuilder(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();}});}}