Evgenii
Evgenii

Reputation: 145

Enable Night mode for Preview by default

I have about three hundred layouts in my application and I want to implement dark theme for it. Currently, I am trying to find out if there any errors that are related to background colors. In order to do that, I looking through all my layouts one by one, opening them in Preview window and clicking "Orientation for preview -> Night mode -> Night". It is slightly ineffective and slow. Is there any way to show xml preview in Night mode by default?

Upvotes: 9

Views: 2138

Answers (1)

Martin Zeitler
Martin Zeitler

Reputation: 76639

The button to switch could indeed be more prominent, as it is not orientation-related.

It could only know about Night Mode when loading the XML from layout-night.
And it is possible to include the whole layout node from layout in layout-night.

Try setting tools:theme="AppTheme.Dark", because includes are not editable.


I've just seen that the opposite is layout-notnight ... https://developer.android.com/guide/topics/resources/providing-resources#table2


With Jetpack Compose Tooling:

implementation "androidx.compose.ui:ui-tooling:1.0.1"

One can define night-mode previews with UI_MODE_NIGHT_YES:

@Preview(name = "Light theme")
@Preview(name = "Dark theme", uiMode = UI_MODE_NIGHT_YES)

Upvotes: 6

Related Questions