How to use and style new AlertDialog from appCompat 22.1 and above How to use and style new AlertDialog from appCompat 22.1 and above android android

How to use and style new AlertDialog from appCompat 22.1 and above


When creating the AlertDialog you can set a theme to use.

Example - Creating the Dialog

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);builder.setTitle("AppCompatDialog");builder.setMessage("Lorem ipsum dolor...");builder.setPositiveButton("OK", null);builder.setNegativeButton("Cancel", null);builder.show();

styles.xml - Custom style

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">    <!-- Used for the buttons -->    <item name="colorAccent">#FFC107</item>    <!-- Used for the title and text -->    <item name="android:textColorPrimary">#FFFFFF</item>    <!-- Used for the background -->    <item name="android:background">#4CAF50</item></style>

Result

styled alertdialog

Edit

In order to change the Appearance of the Title, you can do the following. First add a new style:

<style name="MyTitleTextStyle">    <item name="android:textColor">#FFEB3B</item>    <item name="android:textAppearance">@style/TextAppearance.AppCompat.Title</item></style>

afterwards simply reference this style in your MyAlertDialogStyle:

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">    ...    <item name="android:windowTitleStyle">@style/MyTitleTextStyle</item></style>

This way you can define a different textColor for the message via android:textColorPrimary and a different for the title via the style.


To use a theme for all the application, and don't use the second parameter to style your Dialog

<style name="MyTheme" parent="Base.Theme.AppCompat.Light">    <item name="alertDialogTheme">@style/dialog</item>    <item name="colorAccent">@color/accent</item></style><style name="dialog" parent="Base.Theme.AppCompat.Light.Dialog.Alert">    <item name="colorAccent">@color/accent</item></style>

On my app using a color accent in theme don't show the alertDialog's buttons with the theme colorAccent I have to add a dialog style in the theme.


If you want to use the new android.support.v7.app.AlertDialog and have different colors for the buttons and also have a custom layout then have a look at my https://gist.github.com/JoachimR/6bfbc175d5c8116d411e

@NonNull@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {    View v = inflater.inflate(R.layout.custom_layout, null);    initDialogUi(v);    final AlertDialog d = new AlertDialog.Builder(activity, R.style.AppCompatAlertDialogStyle)            .setTitle(getString(R.string.some_dialog_title))            .setCancelable(true)            .setPositiveButton(activity.getString(R.string.some_dialog_title_btn_positive),                    new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialog, int which) {                            doSomething();                            dismiss();                        }                    })            .setNegativeButton(activity.getString(R.string.some_dialog_title_btn_negative),                    new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialog, int which) {                            dismiss();                        }                    })            .setView(v)            .create();    // change color of positive button             d.setOnShowListener(new DialogInterface.OnShowListener() {        @Override        public void onShow(DialogInterface dialog) {            Button b = d.getButton(DialogInterface.BUTTON_POSITIVE);            b.setTextColor(getResources().getColor(R.color.colorPrimary));        }    });    return d;}

enter image description here