Night Mode color is not applying to the recycler view background

2.6k Views Asked by At

I want to implement night mode for my android app so I have used Theme.AppCompat.DayNight theme for implementing Night Mode. But I have to customize the color of the Toolbar and recycler view during the Night Mode.

For that, I have declared the attribute in the attrs.xml file and use that attribute as a background in the recyclerview.

Here is the attrs.xml file

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ds">
        <attr name="rv_color" format="color"/>
    </declare-styleable>
</resources>

Here is the recyclerview

 <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?attr/rv_color"
        android:overScrollMode="never"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

Now for styles, I have declared styles.xml and styles.xml (night) for Night Mode.

Here is the styles.xml

<style name="AppTheme" parent="Theme.AppCompat.DayNight">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@android:color/white</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:textColorPrimary">@color/colorPrimary</item>
    <item name="android:windowDisablePreview">false</item>
    <item name="rv_color">#FF0000</item>
</style>

Here is the styles.xml (night)

<style name="AppTheme" parent="Theme.AppCompat.DayNight">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@android:color/white</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:textColorPrimary">@color/colorPrimary</item>
    <item name="android:windowDisablePreview">false</item>
    <item name="rv_color">#FFFF00</item>
</style>

In the styles.xml file I have defined RED color for recyclerview background and YELLOW color in the night mode file.

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

The above line is present in the Activity file which to Night Mode.

But Every time the recycler view color is RED i.e. from the styles.xml

Why styles.xml (night) color is not applying to the recyclerview.

Why it is not working? or any other way to do this?

3

There are 3 best solutions below

3
On

I had the same problem (only that the RecyclerView always had the color defined in styles.xml (night), regardless of whether day or night mode was activated) and found a solution in this thread; the only thing I had to change was not to call

getApplicationContext() 

but to use

MyActivity.this 

instead when creating the Adapter with

MyAdapter adapter = new MyAdapter(getApplicationContext(), arrayList);

in the corresponding Activity. Working solution:

MyAdapter adapter = new MyAdapter(MyActivity.this, arrayList);
2
On

Another way would be to create a values-night folder in your resources, place a colors.xml in it and declare a color like "rv_color":

<color name="rv_color">#ffff00</color>

and in the regular values/colors.xml:

<color name="rv_color">#ff0000</color>

Set the color of your recyclerview to that color. No need to declare any custom attribute.

0
On

When I first tried implementing ondzilla's suggestion it didn't work. However, when I set the recyclerview background to another color I already had in the colors.xml file the background now toggles correctly between day/night. I then switched it back to rv_color and it works correctly.

No idea if this will help with yours but I'm super happy you asked this question and that mine now works.