2

We developing home screen widgets in 2021 and I tried to implement dark theme support.

According to https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#widgets

"... use appropriate theme attributes instead of hardcoded colors."

So as I understand I can use custom attributes and I added attrs.xml in values folder

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="customBackgroundColor" format="color" />
</resources>

After that I added my custom attribute to light/dark theme

values/themes.xml

<resources>
    <style name="AppTheme" parent="Theme.MaterialComponents.Light">
        <item name="customBackgroundColor">@color/white</item>
    </style>
</resources>

And also for dark

values-night/themes.xml

<resources>

    <style name="AppTheme" parent="Theme.MaterialComponents">
        <item name="customBackgroundColor">@color/black</item>
    </style>
</resources>

Here is my widget layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/customBackgroundColor" />

When I placing widget on home screen I got "Problem loading widget" message.

When I tried to hardcode colors for layouts, everything works as expected.

I also tried using default attributes "colorPrimary" and there was no error message but widget not changing color according to theme I switched.

Do I missing something or information in official docs is incorrect?

Joks
  • 309
  • 2
  • 17
  • 1
    Since `Theme.MaterialComponents` comes from a library, I don't think you can use one in an app widget definition. The launcher may not have that library. Try using a framework theme as the base for the app widget. – CommonsWare Mar 31 '21 at 19:56
  • @CommonsWare Thanks for the answer. I tried to use parent="@android:style/Theme.Light" for light and "@android:style/Theme" for dark but have the same error message – Joks Mar 31 '21 at 20:19
  • 1
    OK, next thing to try: can you try using an existing platform theme attribute, rather than the custom one? It is certainly possible there is a documentation bug -- there have been plenty of such bugs over the years. However, at the same time, app widget hosts are able to resolve resources, so it is unclear where the breakdown is occurring. BTW, are there any interesting messages in Logcat? – CommonsWare Mar 31 '21 at 20:23
  • @CommonsWare No, unfortunately nothing interesting in logs. I tried to use android:colorBackground and I didn't have error message but after switching theme the background color not changed. – Joks Mar 31 '21 at 20:37
  • 1
    "after switching theme the background color not changed" -- are you sure that your launcher honors dark mode changes? Do other third-party app widgets change on that launcher's home screen? All you can do is make your alternate theme available -- the launcher renders the app widget, and so the launcher needs to have the smarts to re-render the app widget when dark mode changes. – CommonsWare Mar 31 '21 at 20:47
  • @CommonsWare Yes, I am testing on Pixel 3. And I checked "Calendar" widget and "Chrome search" widget and they changing backround color according to dark mode changes. Looks like the only way is to hardcode colors. – Joks Mar 31 '21 at 20:57

0 Answers0