Reputation: 3
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?
Upvotes: 0
Views: 5833
Reputation: 68
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();
Upvotes: 2
Reputation: 1121
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);
Upvotes: 0
Reputation: 1803
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:
Upvotes: 0