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

[M3 Dialog] Hero Icon and Title alignment inconsistent with documentation #2480

Closed charlie-niekirk closed 3 years ago

charlie-niekirk commented 3 years ago

Description: The M3 documentation shows dialogs with so called "Hero Icons" above and aligned centrally with the title. However when creating a dialog with MaterialAlertDialogBuilder the title and Icon are displayed inline to the top left.

Expected behavior:

hero

Source code: This code:

MaterialAlertDialogBuilder(requireContext())
    .setItems(items) { dialog, which ->
          /../
    }
    .setTitle(R.string.pick_image_title)
    .setIcon(R.drawable.ic_image)
    .setNegativeButton(R.string.cancel) { dialog, _ ->
        dialog.dismiss()
    }.show()

Produces: Screenshot_20211124_163622

Android API version: 31

Material Library version: 1.5.0-beta01

Device: Android 12 emulator

charlie-niekirk commented 3 years ago

I've also just found that the documentation clearly states that, with an icon, the content of the dialog should be center aligned:

Screenshot 2021-11-24 at 16 42 43
drchen commented 3 years ago

Hi, so basically it's not feasible for us to support a default style which can alter the layout according to the existence of icons. (We are not able to check default theme/style overriding done by clients and decide if we should change the layout.)

So in Android's context, the spec serves more like a client-side guideline - we suggest you use ThemeOverlay.Material3.MaterialAlertDialog.Centered when create an alert dialog with icons. You can do this by:

MaterialAlertDialogBuilder(requireContext(), R.style.ThemeOverlay_Material3_MaterialAlertDialog_Centered)
drchen commented 3 years ago

FYI we have a bug in M3 dialog styles in the current release which will incorrectly center-align the message text as well. I'm making a fix for that.

charlie-niekirk commented 3 years ago

Thanks for the clarification, that makes a lot of sense!

asthagarg2428 commented 1 year ago

Hi, so basically it's not feasible for us to support a default style which can alter the layout according to the existence of icons. (We are not able to check default theme/style overriding done by clients and decide if we should change the layout.)

So in Android's context, the spec serves more like a client-side guideline - we suggest you use ThemeOverlay.Material3.MaterialAlertDialog.Centered when create an alert dialog with icons. You can do this by:

MaterialAlertDialogBuilder(requireContext(), R.style.ThemeOverlay_Material3_MaterialAlertDialog_Centered)

I've not set(I can't actually change that) the application theme to M3 in Manifest, is there any other way to still use M3 Alert dialog? Otherwise I'm getting the below exception:

android.view.InflateException: Binary XML file line #60 in :layout/m3_alert_dialog: Binary XML file line #60 in :layout/m3_alert_dialog: Error inflating class TextView
                                                                                                    Caused by: android.view.InflateException: Binary XML file line #60 in :layout/m3_alert_dialog: Error inflating class TextView
                                                                                                    Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 5: TypedValue{t=0x2/d=0x7f04012a a=11}, theme={InheritanceMap=[id=0x7f15050bcom.
AlessandroVendraminiDeveloper commented 1 year ago

If you cannot import: R.style.ThemeOverlay_Material3_MaterialAlertDialog_Centered

Try to: com.google.android.material.R.style.ThemeOverlay_Material3_MaterialAlertDialog_Centered