DEV Community

Cover image for Alert Dialog in Android
Sinazo Bogicevic
Sinazo Bogicevic

Posted on

Alert Dialog in Android

A dialog is a small window that pops up on the screen and prompts the user to make a decision before they continue with their action. Think of it as a pop up for android.

Step 1. Create an appropriately named class that extends DialogFragment. It's better to use DialogFragment as it's lifecycle aware and the class will handle any lifecycle events such as the user rotating the screen or pushing the back button.

public class Dialog extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        super.onCreateDialog(savedInstanceState)
   }
}
Enter fullscreen mode Exit fullscreen mode

step 2: Use the builder class as its more convenient. When you instantiate this class and call show, you'll be able to see the dialog.

public class Dialog extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
       AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
       return builder.create();
   }
}
Enter fullscreen mode Exit fullscreen mode

step 3: Set the title and the message you want to display to the user.

public class Dialog extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
       AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
       builder.setTitle(R.string.download_file)
              .setMessage(R.string.dialog_download_file)

       return builder.create();
   }
}
Enter fullscreen mode Exit fullscreen mode

step 4: Now you're going to add the action buttons. There should be no more than three buttons; a negative, positive and neutral action. First, we'll start with the positive action. The user will use this to accept and continue with the action.

public class Dialog extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
       AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
       builder.setTitle(R.string.download_file)
              .setMessage(R.string.dialog_download_file)     
.setPositiveButton(String.valueOf(R.string.download), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
               //accept and continue
            }
        });

       return builder.create();
   }
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Now the last button you will add will be the negative action. The user needs this inorder to cancel the action.

public class Dialog extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
       AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
       builder.setTitle(R.string.download_file)
              .setMessage(R.string.dialog_download_file)
      .setPositiveButton(String.valueOf(R.string.download), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //accept and continue
            }
        })
      .setNegativeButton(String.valueOf(R.string.cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //cancel action
            }
        });

       return builder.create();
   }
}
Enter fullscreen mode Exit fullscreen mode

step 6: Instantiate a Dialog object and call show() making sure you pass in the supportFragmentManager and a tag name for the fragment.

 Dialog dialog = new Dialog();
 dialog.show(getSupportFragmentManager(), "download");
Enter fullscreen mode Exit fullscreen mode

Alt Text

disclaimer
Banner vector created by freepik - www.freepik.com

Top comments (0)