Android Material Components - MaterialAlertDialog With Examples

Material Design 2.0 is out and we can’t wait to get our hands on Dialogs.

In this tutorial, we’ll be customizing Dialogs using Material Theme in our Android Application.

Material Components – Dialogs

Alert Dialogs are a vital part of applications. Typically used to bring user’s attention to something important. Thanks to Material Design 2.0, we have much powerful Dialog now.

First and foremost you need to add the material components dependency:

Do not forget to inherit MaterialComponent Theme or descendants as the Activity Theme.

Basic Implementation

Now let’s create a basic MaterialAlertDialog using the Builder pattern:

This is how it looks on the screen:

android-material-dialog-basic

Android Material Dialog Basic

Let’s compare it with the old alert dialog:

android-material-dialog-basic

Android Material Dialog Old Alert

Surely, the new MaterialAlertDialog looks much better.

AlertDialog loses its content on configuration change. Hence it is recommended to use AppCompatDialogFragment. But for the sake of simplicity of this tutorial, we’ll stick with MaterialAlertDialog.

Styled Buttons

We can style the buttons of MaterialAlertDialog since they are just MaterialButtons.
Setting outline button/borderless button, ripple effects etc.

Let’s see an example:

 

android-material-dialog-button-styled

Android Material Dialog Button Styled

Cut Shaped Dialog

We can set shapes on Material Dialogs now!
Let’s style one as cut shaped by inheriting from the ShapeAppearance style.

Now just set the style in the builder constructor:

 

android-material-dialog-cut-shaped

Android Material Dialog Cut Shaped

Round Shaped Dialog

Setting the above style in the Dialog Builder constructor from the previous code snippet gives us this:

android-material-dialog-cut-shaped

 

Android Material Dialog Round Shaped

Aren’t these designs so drool-worthy!

Custom Font Dialog

Lastly, we can set custom font families on the button and title as well as shown below:

This gives us a completely new look dialog as shown below:

android-material-dialog-typography (1)

 

Android Material Dialog Typography

That brings an end to this tutorial. The font used above is available in the source code attached below.

By admin

Leave a Reply

%d bloggers like this: