Today I’m talking about material style for dialogs in Android application.

STEP 1: ADD LIBRARIES TO THE PROJECT
Firstly need to add libraries to gradle file for a project.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
}

STEP 2: CREATE DIALOG IN APPLICATION
For creating dialog in application you can use AlertDialog component. Better way use “android.support.v7.app.AlertDialog“.
Next snippet of code show how you can create dialog:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.info_title);
builder.setMessage(R.string.info_description);
builder.setPositiveButton(R.string.ok, null);
builder.setNegativeButton(R.string.cancel, null);
builder.show();

In this example we don’t use click action for dialog, if you want do it you must change null to
new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        //SOME CODE
    }
}

This source code use resources from string file.

STEP 3: ADD STRING TO THE APPLICATION
Better way use string resources instead of strings in source code. It’s better for localisation for different languages. For it need to open file strings.xml from folder /res/values/.

<resources>
    <string name="ok">OK</string>
    <string name="cancel">Cancel</string>
    <string name="info_title">Information</string>
    <string name="info_description">Android is a mobile operating system (OS) currently developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets.
From Wikipedia</string>
</resources>

After these steps you can run the application and check dialog

Standart dialog in Android
Standart dialog in Android

STEP 4: CREATE STYLE FOR DIALOG AND APPLY THIS STYLE FOR APPLICATION
For add new style to your project need to open file styles.xml from folder /res/values/ and add style

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">#546E7A</item>
    <item name="android:textColorPrimary">#37474F</item>
    <item name="android:background">#B0BEC5</item>
</style>

colorAccent – color of buttons
textColorPrimary – color of text
background – color of background of dialog

You can set style just for dialog or for all dialogs in application.
1) If you want to set style for dialog you can setup it during creating of AlertDialog.Builder. For it need to change this line

AlertDialog.Builder builder = new AlertDialog.Builder(this);

to
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);

in this case AppCompatAlertDialogStyle it’s a name of style.

2) If you want add some style for all dialogs in your application you can add your style to application style, as example

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <strong><item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item></strong>
    </style>

    <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">#546E7A</item>
        <item name="android:textColorPrimary">#37474F</item>
        <item name="android:background">#B0BEC5</item>
    </style>

Stylized dialog in Android application
Stylized dialog in Android application
4 CommentsClose Comments

4 Comments

  • Rubbi Shole Markovic
    Posted March 12, 2018 at 10:44 am 0Likes

    Hi,

    does this mean that I cannot change colors of a dialog if I am not using graddle?
    I want to change my dialogs so that they are all white with black, large text .

    Can you please help me?

    Thanks.

    RSM

    • Posted July 30, 2018 at 8:12 pm 0Likes

      You can change everything in your dialog without gradle.
      Gradle is a build system which is used for Java, Android, Kotlin, etc.

      You can find and download the ‘com.android.support:appcompat-v7’ library and use it without gradle.

  • Himanshu
    Posted March 22, 2018 at 8:01 am 0Likes

    How to change fontFamily of AlertDialog’s message, title and buttons via styles.xml

    • Posted July 30, 2018 at 8:42 pm 0Likes

      You can use “android:fontFamily” with value, like sans-serif-condensed

Leave a comment