A Material styled AlertDialog

Are you looking for a Material styled AlertDialog?

Here a short gist to migrate the "old" AlertDialog
 AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Dialog");
        builder.setMessage("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo,");
        builder.setPositiveButton("OK", null);
        builder.setNegativeButton("Cancel", null);
        builder.show();

Now you can use the new android.support.v7.app.AlertDialog and the the code becomes:

import android.support.v7.app.AlertDialog;

  AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
        builder.setTitle("Dialog");
        builder.setMessage("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo,");
        builder.setPositiveButton("OK", null);
        builder.setNegativeButton("Cancel", null);
        builder.show();
using this style:

   <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">#FFCC00</item>
        <item name="android:textColorPrimary">#FFFFFF</item>
        <item name="android:background">#5fa3d0</item>
    </style>
and you can enjoy your material styled Dialog on Kitkat:


or on Lollipop:

If you would like tu use a custom style for the title (for example) you can use a style like this:

    <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">#FFCC00</item>
        <item name="android:textColorPrimary">#FFFFFF</item>
        <item name="android:background">#5fa3d0</item>

        <item name="android:windowTitleStyle">@style/TitleDialogTextStyle</item>
    </style>

    <style name="TitleDialogTextStyle" parent="Base.DialogWindowTitle.AppCompat">
        <item name="android:textColor">#d71616</item>
    </style>

Popular posts from this blog

Expand and collapse animation

Android-5: Card and images with rounded corners in Android 4

How to centralize the support libraries dependencies in gradle