Android
AlertDialog
Theme Customization
UI Design
Android Development

How to change theme for AlertDialog

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Changing AlertDialog theme in Android is best handled through style resources and themed context wrappers, not ad hoc runtime tweaks. A consistent theme approach keeps typography, colors, and shape behavior predictable across dialogs. Modern apps should usually use Material components for better compatibility and accessibility.

Choose the Right Dialog API

There are two common approaches:

  • AlertDialog.Builder from AppCompat
  • MaterialAlertDialogBuilder from Material Components

If your app already uses Material theme, prefer MaterialAlertDialogBuilder for consistent look and behavior.

kotlin
1import android.os.Bundle
2import androidx.appcompat.app.AppCompatActivity
3import com.google.android.material.dialog.MaterialAlertDialogBuilder
4
5class MainActivity : AppCompatActivity() {
6    override fun onCreate(savedInstanceState: Bundle?) {
7        super.onCreate(savedInstanceState)
8
9        MaterialAlertDialogBuilder(this, R.style.ThemeOverlay_MyApp_Dialog)
10            .setTitle("Delete item")
11            .setMessage("This action cannot be undone.")
12            .setPositiveButton("Delete") { _, _ -> }
13            .setNegativeButton("Cancel", null)
14            .show()
15    }
16}

The second constructor argument applies a specific theme overlay to that dialog.

Define a Dialog Theme Overlay

Create a style that customizes dialog appearance and references Material attributes.

xml
1<!-- res/values/styles.xml -->
2<resources>
3    <style name="ThemeOverlay.MyApp.Dialog" parent="ThemeOverlay.Material3.MaterialAlertDialog">
4        <item name="colorPrimary">@color/teal_700</item>
5        <item name="materialAlertDialogBodyTextStyle">@style/MyDialogBodyText</item>
6        <item name="shapeAppearanceMediumComponent">@style/MyDialogShape</item>
7    </style>
8
9    <style name="MyDialogBodyText" parent="TextAppearance.Material3.BodyMedium">
10        <item name="android:textColor">@color/gray_900</item>
11    </style>
12
13    <style name="MyDialogShape" parent="ShapeAppearance.Material3.MediumComponent">
14        <item name="cornerSize">20dp</item>
15    </style>
16</resources>

This keeps design tokens centralized and reusable.

Apply Theme Globally or Per Dialog

If most dialogs should match one look, set global theme attributes in your app theme. Use per-dialog overlays only for exceptions such as destructive confirmation or branded onboarding dialogs.

xml
<style name="Theme.MyApp" parent="Theme.Material3.DayNight.NoActionBar">
    <item name="materialAlertDialogTheme">@style/ThemeOverlay.MyApp.Dialog</item>
</style>

Global assignment reduces repetitive builder code and prevents style drift.

Handling Legacy AlertDialog.Builder

Legacy code can still work with ContextThemeWrapper.

kotlin
1import android.view.ContextThemeWrapper
2import androidx.appcompat.app.AlertDialog
3
4val themedContext = ContextThemeWrapper(this, R.style.ThemeOverlay_MyApp_Dialog)
5AlertDialog.Builder(themedContext)
6    .setTitle("Legacy dialog")
7    .setMessage("Still themed through wrapped context.")
8    .setPositiveButton("OK", null)
9    .show()

This is useful during gradual migration to Material components.

Advanced Styling and Action Button Control

Beyond title and body typography, many teams need custom action button emphasis and icon behavior. Keep these customizations theme-driven when possible.

xml
1<style name="ThemeOverlay.MyApp.Dialog" parent="ThemeOverlay.Material3.MaterialAlertDialog">
2    <item name="buttonBarPositiveButtonStyle">@style/MyDialogPositiveButton</item>
3    <item name="buttonBarNegativeButtonStyle">@style/MyDialogNegativeButton</item>
4</style>
5
6<style name="MyDialogPositiveButton" parent="Widget.Material3.Button.TextButton.Dialog">
7    <item name="android:textColor">@color/green_700</item>
8</style>
9
10<style name="MyDialogNegativeButton" parent="Widget.Material3.Button.TextButton.Dialog">
11    <item name="android:textColor">@color/red_700</item>
12</style>

At runtime, avoid deep view traversal hacks to style internal dialog views. Those are brittle across library updates. Use documented theme attributes and builder APIs first.

For design systems, define dialog variants such as info, warning, and destructive. Reference each variant through a dedicated overlay style so product teams can apply consistent semantics quickly.

When testing dialog themes, verify typography scale, button contrast, and corner shapes on both compact and large screens. Also check right-to-left locales to ensure spacing and alignment still feel intentional. These checks catch subtle regressions that do not appear in default emulator settings.

For mature apps, consider screenshot testing of key dialog variants as part of CI to detect style drift immediately after theme refactors. Visual diff checks are especially useful when library upgrades change default paddings or typography metrics.

Common Pitfalls

A common pitfall is using a dialog overlay that does not match the app base theme family. Mixing Material2 and Material3 styles can produce unexpected colors or padding.

Another issue is styling one dialog directly in code while others use theme resources. Long-term consistency suffers.

Developers also forget dark mode checks. Dialog text and surface colors may pass in light mode but fail contrast requirements in dark mode.

Finally, avoid hardcoded color literals in dialog builder code. Put theme values in resources and reference attributes.

Summary

  • Prefer Material dialog APIs when using Material app themes.
  • Use theme overlays to centralize dialog visual behavior.
  • Apply global dialog theme for consistency, then override selectively.
  • Use wrapped context for legacy migrations.
  • Validate contrast and spacing across light and dark themes.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.