0

I want to create an AlertDialog with buttons that look like this: https://developer.android.com/images/ui/dialogs_regions.png

However, whenever I create an AlertDialog with the AlertDialog.Builder, I end up getting buttons that look like this: https://developer.android.com/images/ui/dialog_buttons.png

How can I change the AlertDialog so that the buttons look like the first example (i.e. taking up the entire bottom of the dialog, with gray divider lines between each button)? Note that I am not trying to change the color of the dialog window, just the way that the buttons appear on it. Ideally, I'd like to do this with just the default android styles and without defining a custom style - is this possible?

Here's the code I use to create and display the AlertDialog:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

alertDialogBuilder.setTitle("Include answers in summary?");
alertDialogBuilder.setMessage("You have completed " + String.valueOf(questionsCompleted) + " out of 18 questions. Would you like the summary to include these answers along with the questions?");
alertDialogBuilder.setCancelable(false);

alertDialogBuilder.setPositiveButton("Include", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Intent intent = new Intent(getApplicationContext(), SummaryActivity.class);
        intent.putExtra("componentNumber", 0);
        intent.putExtra("includeAnswers", true);
        startActivity(intent);
        }
    });

alertDialogBuilder.setNegativeButton("Omit", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Intent intent = new Intent(getApplicationContext(), SummaryActivity.class);
        intent.putExtra("componentNumber", 0);
        intent.putExtra("includeAnswers", false);
        startActivity(intent);
    }
});

alertDialogBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
});

AlertDialog alertDialog = alertDialogBuilder.create();

alertDialog.show();

There are three actions in this AlertDialog. Without getting too much into the details, two of the buttons take the user to the same activity summarizing a questionnaire, but with or without their answers included, and the other button cancels the dialog. I know I'm supposed to use the NegativeButton for canceling the dialog, but even though the android developer guide on dialogs says that a neutral button will appear between the positive and negative buttons, I've been getting the order Neutral->Negative->Positive. As a result, I've been getting a dialog with the Omit option on the left side and the Cancel and Include options on the right side, which is extremely unintuitive to me.

I'd like to change the order of the buttons to be Negative->Neutral->Positive and define the negative button as canceling the dialog so that the two buttons which lead to the summary activity are grouped together - is this at all possible?

Tommy Peele
  • 3
  • 1
  • 3
  • why not just replace the codes to correct order? Put that method you want first to the button that appears first and set the related button text. The user does not know if it is a neutral, positive or negative button. – Opiatefuchs Jun 14 '17 at 02:47
  • and if you want a dialog that is styled to your needs, just make a custom dialog. You can make a layout like you do with activities...https://www.mkyong.com/android/android-custom-dialog-example/ – Opiatefuchs Jun 14 '17 at 02:49
  • I tried rearranging the button assignments in the code, but it had no effect. The reason I'm worried about using the neutral button for the cancel action is that I'm afraid different phones might display the buttons differently so the user might enter an activity when they are actually trying to cancel the dialog. I guess using a custom dialog would solve that problem, though, so I'll give it a shot - thanks for the tutorial link. – Tommy Peele Jun 14 '17 at 03:00
  • there are several things to be aware of a custom dialog, but it´s worth the work. You must eventually specify the size of the dialog: https://stackoverflow.com/questions/10242144/adjusting-size-of-custom-dialog-box-in-android and modify the title view or remove it if you do it by yourself in the layout: https://stackoverflow.com/questions/2644134/android-how-to-create-a-dialog-without-a-title – Opiatefuchs Jun 14 '17 at 04:22

3 Answers3

2

I believe my answer will be the most helpful for you. Just create a layout for your dialog like the one below which I believe is exactly what you want.

<?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"
        android:divider="?android:dividerVertical"
        android:showDividers="middle">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Are you sure?"
    android:textStyle="bold"
    android:textSize="20sp"
    android:gravity="center"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:id="@+id/dialog_text"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    style="?android:buttonBarStyle"
    android:divider="?android:dividerHorizontal"
    android:showDividers="middle">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/cancel_action"
        android:layout_weight="1"
        android:text="Cancel"
        android:textAllCaps="false"
        style="?android:buttonBarButtonStyle"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/ok_action"
        android:layout_weight="1"
        android:text="Ok"
        android:textAllCaps="false"
        style="?android:buttonBarButtonStyle"/>
</LinearLayout>

</LinearLayout>

And then set your dialog's layout in code:

final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
    View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_layout, null);
    dialog.setView(view);
    final AlertDialog alert = dialog.create();
    Button cancel = (Button) view.findViewById(R.id.cancel_action);
    cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            alert.dismiss();
        }
    });
    Button ok = (Button) view.findViewById(R.id.ok_action);
    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //whatever you want

        }
    });
    alert.show();
C. Scott
  • 68
  • 4
  • This is exactly what I had in mind! I'm going to use this as a base to make a two-button and three-button version so I can apply the layout to every dialog in my app. Thanks a ton! – Tommy Peele Jun 14 '17 at 20:26
  • As a final quick question, is there a way to create a dialog like this with a title above the message, but without a divider line between the title and message? In other words, I'd like to keep the divider lines as they are between the buttons and message area, but remove the line between the title area and message area. – Tommy Peele Jun 14 '17 at 20:31
  • Sorry, I just noticed one more thing. When I press one of the buttons that takes me to the next activity, then press the back button in that activity, the dialog window is still open when I return to the original activity. Is there a way to force the dialog window to close when these buttons are pressed, or when the original view is reloaded? The default dialog window I was using before did not exhibit this behavior and would be closed whenever I returned to its activity, so I'm a bit stumped as to how to fix this. – Tommy Peele Jun 14 '17 at 20:48
  • Ill have to look into your first question more, but as for your second question, simply call alert.dismiss before you launch the new activity in the onClick of whatever button you are pressing – C. Scott Jun 14 '17 at 20:52
0

The AlertDialog buttons' appearances are based on respective Android API versions, so what you want (based on your first screenshot) is an AlertDialog in Android 4.4.2 (KitKat) with an app theme of Theme.AppCompat - I'd know this because that's how it appears in my tablet 4.4.2. For instance: enter image description here

DaveNOTDavid
  • 1,753
  • 5
  • 19
  • 37
  • That's what I feared was the case. Oh well, I'll just make a custom dialog like Opiatefuchs suggested. Thanks for clearing that up, though! – Tommy Peele Jun 14 '17 at 03:02
0

To load AlertDialog in different or specified theme, you can try something like:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo_Light_Dialog)));

It's available from API 1.

Also, about the button sequence, you can just switch your code to get the desired result on button click, ignoring the type of button. Something like:

builder.setNeutralButton("NO",listener);
builder.setNegativeButton("NOT SURE",listener);
builder.setPositiveButton("YES",listener);
DsD
  • 1,081
  • 8
  • 11