7

I what to change background as night mode changes.

I have /values and /values-night folder, that contain "colors.xml" with different values. `

<color name="grey1">#ebebeb</color>
<color name="grey2">#c7c7c7</color>
<color name="grey3">#999999</color>
<color name="hover1">#8bb065</color>
<color name="red1">#ba0000</color>
<color name="red2">#ff0000</color>
<color name="green1">#336600</color>
<color name="text1">#000000</color>

and other is

<color name="grey1">#999999</color>
<color name="grey2">#333333</color>
<color name="grey3">#000000</color>
<color name="hover1">#8bb065</color>
<color name="red1">#ba0000</color>
<color name="red2">#ff0000</color>
<color name="green1">#336600</color>
<color name="text1">#ffffff</color>

these colors are used in layer list for background "activity_main_bg2.xml":

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/grey1" />
        </shape>
    </item>
    <item
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp">
        <shape android:shape="rectangle" >
            <solid android:color="@color/grey2" />
        </shape>
     </item>
</layer-list>

My activity contains fragment:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world_dark"
    android:background="@drawable/activity_main_bg2" />

When I change time from day-night or back colors in background does not change. But if I use

android:background="@color/grey1"

everythings works ok.

How to solve this? Is this android bug?

Solata
  • 1,444
  • 3
  • 28
  • 42
  • Are you sure `UiModeManager.getNightMode()` returns `MODE_NIGHT_AUTO`? – Mohamed_AbdAllah Oct 20 '13 at 11:30
  • @Mohamed_AbdAllah: Yes, I am sure. As stated in last sentence, if I set color to background, it works as it should. That's not the case, if I set [layer-list](http://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList). – Solata Oct 21 '13 at 08:38

1 Answers1

9

Please try this:

  1. Create file style.xml in folder res/values and res/values-night.
  2. Add to both of them style for your TextView like:

    <style name="textViewStyle">
    <item name="android:background">@drawable/activity_main_bg2</item>
    </style>
    
  3. Update your layout to:

    <TextView
    style="@style/textViewStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world_dark" />
    

This solution should get property color dependent of day/night mode.

mamakurka
  • 480
  • 1
  • 4
  • 14
  • 2
    Great alternative solution! But why we must have copy of style.xml in both res/values AND res/values-night? From documentation, if res xml is not found in /values-night it should take the one from /values. I would realy like to know why this happens!? – Solata Oct 22 '13 at 07:19
  • It seems copy the activity_main_bg2.xml to drawable-night, these two xml have the same content, also works fine. But i think it should not need to do that. – peerless2012 Dec 06 '19 at 08:18