Search code examples
androidandroid-alertdialogandroid-themeandroid-styles

Android: AlertDialog background appears different based on API


I just noticed that the AlertDialogs that I created and tested on my Galaxy S8+(API 24) appear differently when I test them on Galaxy Note 4(API 19). I created a custom style for the dialog and it seems like on the older API, both the default alert dialog and the custom theme are being displayed.

Here are the pictures demonstrating this.

Dialog on S8+ appears as expected Dialog on S8+

Dialog on Note 4 have both my custom background and the default one Dialog on Note 4

And here is my style and the AlertDialog code

AlertDialog style

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">#0053f9</item>
        <item name="android:textColor">#000000</item>
        <item name="android:windowBackground">@drawable/background_color_blue_black_border</item>
        <item name="buttonBarStyle">@style/Widget.AppCompat.ButtonBar</item>
        <item name="buttonBarButtonStyle">@style/customButtonForMyDialogTheme</item>
    </style>

AlertDialog code

android.app.AlertDialog.Builder a_builder = new android.app.AlertDialog.Builder(RemoteControlBubblePillar.this, R.style.MyDialogTheme);
            a_builder.setMessage("Do you want to turn on the Bubble Pillar ?")
                    .setCancelable(false)
                    .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            //Do some stuff
                        }
                    })
                    .setNegativeButton("No",new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
            android.app.AlertDialog alert = a_builder.create();
            alert.setTitle("Bubble Pillar Is Turned Off");
            alert.show();

I tried setting the background to transparent in style as well as setting it to transparent in the code but nothing works so far. Any idea why this is happening?


Solution

  • Use

    android.support.v7.app.AlertDialog

    instead of

    android.app.AlertDialog

    Set your custom view in dialog .Below is an example.

       AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
    dialogBuilder.setView(dialogView);
    dialogBuilder.setTitle("Custom dialog");
    dialogBuilder.setMessage("Enter text below");
    dialogBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //do something with edt.getText().toString();
        }
    });
    dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //pass
        }
    });
    AlertDialog b = dialogBuilder.create();
    b.show();