Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup night mode drawables to work as expected

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?

like image 376
Solata Avatar asked Oct 15 '13 11:10

Solata


People also ask

When did Android get dark mode?

And one of the major announcements of 2019 was definitely the introduction of Dark Theme on Android during the I/O, and right after that on iOS during the WWDC (called the Dark Mode in Cupertino).

How do I get rid of Dark theme on Android?

Turn Dark theme on or off in your phone's settings On your phone, open the Settings app. Tap Display. Turn Dark theme on or off.

How do you add Drawables?

Drag and drop your images directly onto the Resource Manager window in Android Studio. Alternatively, you can click the plus icon (+), choose Import Drawables, as shown in figure 3, and then select the files and folders that you want to import. Figure 3: Select Import Drawables from the dropdown menu.


1 Answers

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.

like image 61
mamakurka Avatar answered Sep 19 '22 12:09

mamakurka