Uploaded February 2017 | Updated September 2026, 2 weeks ago
Create an AlertDialog using AppCompatDialogFragment. Interact with the parent activity by setting text in a TextView, and by closing it with .finish().
Steps:
- Setup MainActivity with a TextView to edit, and a button to trigger the dialog box.
- Create a layout XML file for the dialog's appearance
- Create a Java class inheriting from AppCompatDialogFragment to build the AlertDialog
- Show the AlertDialog from main using a fragment manager.
- Add a cancel button
- Call .finish() on the parent activity to close it.
Create an AlertDialog using AppCompatDialogFragment. Interact with the parent activity by setting text in a TextView, and by closing it with .finish().
Steps:
- Setup MainActivity with a TextView to edit, and a button to trigger the dialog box.
- Create a layout XML file for the dialog's appearance
- Create a Java class inheriting from AppCompatDialogFragment to build the AlertDialog
- Show the AlertDialog from main using a fragment manager.
- Add a cancel button
- Call .finish() on the parent activity to close it.



