Alert Dialog Usage

ID

kotlin.android_alert_dialog_usage

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Best Practice

Language

Kotlin

Tags

android, best-practice, material-design

Description

Reports calls that construct an AlertDialog or AlertDialog.Builder from the legacy frameworks (android.app.AlertDialog, androidx.appcompat.app.AlertDialog). Material Design Components recommend MaterialAlertDialogBuilder from com.google.android.material.dialog for any dialog surface in a Material 3 / Material You application.

Rationale

val b1 = AlertDialog.Builder(context)            // FLAW — legacy builder
val d1 = AlertDialog(context)                    // FLAW — legacy ctor

val b2 = MaterialAlertDialogBuilder(context)     // OK — Material builder
    .setTitle("Confirm")
    .setMessage("Continue?")
    .show()

The classic builders predate the design system. They ignore dynamic colours, use the legacy dialog corner radii and padding, and do not pick up the typography overlays declared in the Material theme.

Remediation

  • Replace AlertDialog.Builder(ctx) with MaterialAlertDialogBuilder(ctx) from com.google.android.material.dialog.

  • Add the com.google.android.material:material dependency if it is not already present.

  • For non-Material projects, the rule may be disabled via policy.