question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[dialog] ?attr/colorControlHighlight used for android:background

See original GitHub issue

Description: 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:

  1. 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>
  1. In your app theme set colorControlHighlight:
<item name="colorControlHighlight">@color/primary_60.xml</item>
  1. Create and show a Material dialog:
MaterialAlertDialogBuilder(context)
    ...
    .show()
  1. 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 in android:background,
  • use a different color - why would a ripple color be used for backgrounds?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
drchencommented, May 4, 2022

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.

0reactions
SergeyKudryashovcommented, May 4, 2022

Isn’t fixed in the version 1.6.0-rc01 for MaterialComponents.

Read more comments on GitHub >

github_iconTop Results From Across the Web

android - How can I modify ripple color when using ?attr ...
Change colorControlHighlight value for a different color ... Use the foreground attribute as selectableItemBackground and background ...
Read more >
Android styling: common theme attributes | by Nick Butcher
But what theme attributes are available to use? This post highlights the common ones that you should know about; those that come from ......
Read more >
R.attr - Android Developers
A drawable to use as the background. int, backgroundDimAmount. Default background dim amount when a menu, dialog, or something similar pops up.
Read more >
core/res/res/values/themes_material.xml - Google Git
<!-- Material theme for an activity that is to be used for voice interaction. This gives the activity a floating dialog style, to...
Read more >
Migrating to Material Components for Android
If you used the Android Studio 'Refactor > Migrate to AndroidX' tool ... These attributes are used by MDC widgets to tint their...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found