DayNight mode not working for Presentation dialog on second screen

42 Views Asked by At

I've got a small test app to test day/night themes in a device with two screens.

I've got a themes.xml

    <style name="Theme.MyApp" parent="Theme.AppCompat.DayNight.NoActionBar">
    </style>

   <style name="Theme.MyApp.CFS">
        <!-- NOTE: The theme is loading the light mode...-->
        <item name="android:colorBackground">@color/vivid_tangerine</item>
    </style>

And the same theme.xml in values-night folder, with a different color

   <style name="Theme.MyApp" parent="Theme.AppCompat.DayNight.NoActionBar">
    </style>

   <style name="Theme.MyApp.CFS">
        <!-- NOTE: The theme is loading the dark mode...-->
        <item name="android:colorBackground">@color/malibu_2</item>
    </style>

My MainActivity extends AppCompatActivity and has this code on the onCreate

protected void onCreate(Bundle savedInstanceState) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        @SuppressLint("WrongConstant") DisplayManager displayManager = (DisplayManager) getApplicationContext().getSystemService(Context.DISPLAY_SERVICE);
        var presentationDisplays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
        var pres = new Pres(this, presentationDisplays[0], R.style.Theme_MyApp_CFS);
        pres.show();
    }

The theme defined for it is the Theme.MyApp, set in the manifest file.

This Pres class is just a class that extends Presentation and sets the content view to a layout that's using the colorBackground attribute and showing on the secondary display.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/second_screen_theme_wrapper"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:attr/colorBackground"
    android:orientation="vertical">
...

The problem is that the presentation doesn't respect the night mode toggle and shows the theme as if it were the light mode. The main activity screen shows the theme with the correct night mode applied (shows the according bg color) but the presentation/dialog doesn't.

Is there a way to pass the theme file that's in night mode specifically or something else? Or do I need to create two themes on values/theme.xml for day and night mode and pass one to the constructor of the presentation accordingly?

The minSdk is set to 21 and targetSdk to 33, in case that's relevant.

1

There are 1 best solutions below

2
Antony Aiwin K X On

I think its not working because you defined 'android:colorBackground' in a different style named 'Theme.MyApp.CFS'. Just define it in the theme itself and it should work fine.

    <style name="Theme.MyApp" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- NOTE: The theme is loading the light mode...-->
        <item name="android:colorBackground">@color/vivid_tangerine</item>
    </style>

instead of :

   <style name="Theme.MyApp" parent="Theme.AppCompat.DayNight.NoActionBar">
   </style>

   <style name="Theme.MyApp.CFS">
        <!-- NOTE: The theme is loading the light mode...-->
        <item name="android:colorBackground">@color/vivid_tangerine</item>
   </style>

Method 2 :

You can achieve this with another method. You have defined the attribute in a separate style named 'Theme.MyApp.CFS'. You can add the style attribute instead of background attribute to LinearLayout.

   <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/second_screen_theme_wrapper"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        style="@style/Theme.MyApp.CFS">
   ...