Reputation: 180
I have an Alert Dialog that currently looks like this: Current Alert Dialog
But I want the "YES" and "NO" buttons to have a background, or look like they are buttons instead of just text. I have tried implementing a custom theme for AlertDialog, but the only changes I can get is changing the "WARNING" text color.
What would be the most efficient and simple way to customize the theme of the Alert Dialog so that there will be actual buttons in place of "YES" and "NO"?
This is my current res/values/styles.xml file
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorAccent">@color/PCCA_blue</item>
</style>
<style name="customAlertDialog" parent="@android:style/Theme.Holo.Dialog">
<item name="android:textColor">@color/red</item>
</style>
And this is the where I build and show the AlertDialog:
private void alert(final String action){
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.customAlertDialog));
String message = "";
switch(action) {
case "upload":
message = "Are you sure you want to upload the counted quantities?\n\nIt may take a few minutes to reflect your changes.";
break;
case "download":
message = "Downloading new worksheets may overwrite existing counted quantities, do you want to continue?\n\n" +
"NOTE: You may want to upload counts first.\n\n" +
"(Changed counted quantities will still be available for upload)";
break;
default:
message = action;
break;
}
alertDialogBuilder.setTitle("WARNING");
alertDialogBuilder.setMessage(message);
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == DialogInterface.BUTTON_POSITIVE) {
if (action.equals("upload")) {
MainActivity.upload(context);
} else if (action.equals("download")) {
MainActivity.download(context);
}
} else {
dialog.cancel();
}
}
};
alertDialogBuilder.setPositiveButton("YES", listener);
alertDialogBuilder.setNegativeButton("NO", listener);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
Any advice is appreciated. I am new to android.
Upvotes: 3
Views: 1446
Reputation: 22637
AlertDialog has a getButton()
method. You could get the button and set it's background. There are some caveats: you can only call that method after the dialog is shown (I believe after onStart()
has been called on the Dialog class).
That being said, it's likely you won't get the look you want considering paddings, margins, containing layouts, etc. The point of AlertDialog
is to have a standard looking dialog that matches the system theme. It's not meant to be customizable, UI-wise.
Otherwise, you'll need to create a completely custom dialog. You have a few options for that: DialogFragment, custom Dialog, or dialog-themed activity. Google those topics for more information.
Upvotes: 1