[dialog] ?attr/colorControlHighlight used for android:background
See original GitHub issueDescription: mtrl_alert_dialog.xml
and m3_alert_dialog.xml
use ?attr/colorControlHighlight
for android:background
?attr/colorControlHighlight
is a color value while android:background
expects a drawable. Using simple color values for android:background
works but using a color selector causes a crash because it’s being interpreted as a drawable.
This makes it impossible to set color ripples to anything else than primitive color values. A common thing is to use a color selector to apply an alpha to another color attribute as explained e.g. here: https://material.io/blog/android-material-theme-color (section “Color with alpha”).
So basically following the guidelines from the Material Design blog with Material Components for Android leads to a crash when showing dialogs 👍
Expected behavior: No crash
Source code:
- Create a color selector, e.g.
<!-- In res/color/primary_60.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="0.6" android:color="?attr/colorPrimary" />
</selector>
- In your app theme set
colorControlHighlight
:
<item name="colorControlHighlight">@color/primary_60.xml</item>
- Create and show a Material dialog:
MaterialAlertDialogBuilder(context)
...
.show()
- Crash
Android API version: 30 - possibly all 26 and 28, possibly 28 and lower. 29+ works fine.
Material Library version: master, the issue can be clearly seen in source code. Search for colorControlHighlight
in mrtl_alert_dialog.xml
and m3_alert_dialog.xml
files in this repository.
Device: any? reproducible on an emulator.
Suggested fixes:
- define a proper color drawable that uses
?attr/colorControlHighlight
and set that inandroid:background
, - use a different color - why would a ripple color be used for backgrounds?
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
Yeah the fix is only applied to Material 3.
It’s not a trivial fix with Material 2 and also Material 2 is basically frozen now. Please migrate to Material 3 or avoid using CSL with colorControlHighlight.
Isn’t fixed in the version 1.6.0-rc01 for MaterialComponents.