Milack Milack - 1 year ago 164
Android Question

Custom Alert Dialog looking weird on Android 4.x

I want to customize the alert dialog in my Android app, so I started by changing the alertDialogTheme attribute of my app's theme as follows:


<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:alertDialogTheme">@style/AlertDialogTheme</item>

<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert"/>

Just by doing that, I didn't expected the dialog to look differently from the standard, because the new theme AlertDialogTheme is exactly the same as its parent. It worked properly on Android Lollipop, but in Android Kitkat a white rectangle appears behind the dialog. Here's how it looked like:

enter image description here

Am I doing anything wrong? Maybe using the wrong parent?


Answer Source

This happens because you´re using AlertDialog instead of the relatively new AlertDialogCompat. This one brings the same Material Design style dialogs across all platforms and is highly encouraged its use.

To use it, you must be sure that in your Gradle file you have the following line inside your dependencies:
(This one is the latest version. Make sure you have all your packages updated by looking into the SDK Manager).

Then in the Activity where you want to show it you must import and use the AlertDialog.Builder exactly as you were using it before.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download