Tutorials  /  JavaScript

MaterialAlertDialog: Boost Android UX

Ccentron Redaktion · June 2024 ·5 min read ·JavaScript, Tutorial

With the release of Material Design 2.0, Android developers have exciting new options for crafting dialogs that grab user attention and enhance app interactivity. In this guide, we’ll explore how to implement and customize dialogs using the Material Theme in your Android applications.

Understanding Material Components: Dialogs

Dialogs play a crucial role in applications by highlighting essential information or requiring user decisions. Thanks to the advancements in Material Design 2.0, dialogs are now more dynamic and versatile. To get started, include the Material Components library in your project by adding the following dependency:

groovy
implementation 'com.google.android.material:material:1.1.0-alpha09'

Additionally, ensure that your activity inherits from a MaterialComponent theme or one of its descendants to leverage the full range of features.

Building a Basic MaterialAlertDialog

Let’s start by creating a basic MaterialAlertDialog using the Builder pattern:

java
new MaterialAlertDialogBuilder(MainActivity.this)
    .setTitle("Title")
    .setMessage("Your message goes here. Keep it short but clear.")
    .setPositiveButton("GOT IT", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            // Positive button action
        }
    })
    .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            // Negative button action
        }
    })
    .show();

This dialog provides a simple interface for users to acknowledge or cancel an action.

Customizing Button Styles

The buttons in a MaterialAlertDialog can be styled to suit your application’s design theme. Define a dialog theme in res/values/styles.xml that references your own styles for the positive and the negative button:

xml
<style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
    <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
</style>
xml
<style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button">
    <item name="backgroundTint">@color/colorPrimary</item>
    <item name="android:textColor">@android:color/white</item>
</style>
xml
<style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton">
    <item name="android:textColor">@color/colorPrimary</item>
</style>

Apply these styles in the dialog builder:

java
new MaterialAlertDialogBuilder(MainActivity.this, R.style.AlertDialogTheme)
    .setTitle("Title")
    .setMessage("Your message goes here. Keep it short but clear.")
    .setPositiveButton("GOT IT", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            // Positive button action
        }
    })
    .setNeutralButton("LATER", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            // Neutral button action
        }
    })
    .show();

Exploring Dialog Shapes

Material Design allows dialogs to be styled with various shapes, such as cut or rounded corners, to enhance visual appeal.

Cut-Shaped Dialog

The shape is controlled by a shapeAppearanceOverlay that you set on the dialog theme:

xml
<style name="CutShapeTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="shapeAppearanceOverlay">@style/CutShapeAppearance</item>
</style>
xml
<style name="CutShapeAppearance" parent="">
    <item name="cornerFamily">cut</item>
    <item name="cornerSize">16dp</item>
</style>

Apply the cut shape style:

java
new MaterialAlertDialogBuilder(MainActivity.this, R.style.CutShapeTheme)
    .setTitle("Title")
    .setMessage("Your message goes here. Keep it short but clear.")
    .setPositiveButton("GOT IT", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            // Positive button action
        }
    })
    .setNeutralButton("LATER", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            // Neutral button action
        }
    })
    .show();

Rounded-Shaped Dialog

For rounded corners, simply swap the cornerFamily:

xml
<style name="RoundShapeTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="shapeAppearanceOverlay">@style/RoundShapeAppearance</item>
</style>
xml
<style name="RoundShapeAppearance" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">20dp</item>
</style>

Integrate the rounded shape in the builder:

java
new MaterialAlertDialogBuilder(MainActivity.this, R.style.RoundShapeTheme)
    .setTitle("Title")
    .setMessage("Your message goes here. Keep it short but clear.")
    .setPositiveButton("GOT IT", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            // Positive button action
        }
    })
    .setNeutralButton("LATER", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            // Neutral button action
        }
    })
    .show();

Adding Custom Typography

Custom fonts can further personalize your dialog’s appearance:

xml
<style name="CustomFontTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="android:fontFamily">@font/your_custom_font</item>
</style>

Utilize the custom font style to give your dialog a unique touch.

By following these steps, you can create engaging, modern dialogs that adhere to the Material Design principles and enhance the user experience within your Android applications. Experiment with different styles and shapes to find the best fit for your app’s theme!

Source: digitalocean.com

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.