material-components / material-components-android

Modular and customizable Material Design UI components for Android
Apache License 2.0
16.43k stars 3.08k forks source link

[MaterialAlertDialog] `materialAlertDialogTitleTextStyle`'s `textSize` has an arbitrary upper limit #3014

Closed ChristianGarcia closed 1 year ago

ChristianGarcia commented 2 years ago

Description

When styling MaterialAlertDialog via theming, the title's (materialAlertDialogTitleTextStyle) textSize attribute will ignore any values over some arbitrary limit (~20sp).

This works just fine for the dialog's body (materialAlertDialogBodyTextStyle).

I've tested this both using MaterialComponents.* and Material3.* styles (see Minimal sample app repro below).

Expected behavior

The textSize value set for materialAlertDialogTitleTextStyle is respected.

Source code

themes.xml

<style name="Theme.ThemeOverlay.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="materialAlertDialogTitleTextStyle">@style/DialogTitleStyle</item>
    <item name="materialAlertDialogBodyTextStyle">@style/DialogBodyStyle</item>
</style>

<style name="DialogTitleStyle" parent="@style/MaterialAlertDialog.MaterialComponents.Title.Text">
    <!-- For some reason the textSize below is upper-limited to something just under 20sp, so the actual font size will be smaller than that.
    You can try setting this to 100sp (won't work) and 10sp (will work) to see this (buggy) behavior in the material library -->
    <item name="android:textSize">100sp</item>
    <item name="android:textColor">#7f7fff</item>
</style>

<style name="DialogBodyStyle" parent="@style/MaterialAlertDialog.MaterialComponents.Body.Text">
    <item name="android:textSize">50sp</item>
    <item name="android:textColor">#7f7fff</item>
</style>
MaterialAlertDialogBuilder(this, R.style.Theme_ThemeOverlay_MaterialAlertDialog)
    .setTitle("This text's size has a magical upper limit!")
    .setMessage("This text's size works just fine")
    .setPositiveButton("Ok", null)
    .show()

Minimal sample app repro

https://github.com/ChristianGarcia/MaterialDialogBug

Environment

Android API version : API 25

Material Library version: Broken all the way from 1.1.0 (No Material3 on that one) to latest at the time of reporting (1.8.0-alpha01).

Device: Several emulators, Pixel 6 Pro (seems to be device-independent)

drchen commented 1 year ago

That's how AppCompat's AlertDialog works: https://android.googlesource.com/platform/frameworks/base/+/de47f1c358c8186ff3e14b887d5869f69b9a9d6c/core/java/com/android/internal/widget/DialogTitle.java#54

I believe we want to respect the existing behavior of AppCompat's alert dialogs as much as possible. : )