8

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?

Evgenii
  • 135
  • 4
  • Please take a look at this [Implementing Dark Mode in Android](https://blog.mindorks.com/implementing-dark-mode-theme-in-android) – Rahul Gaur Feb 08 '20 at 12:37
  • 1
    @RahulGaur, I am not sure if this is what I am looking for. As far as I can, it explains how to implement dark them. I have already done it. I want to be able to display Preview window in Android studio in Night mode without clicking my mouse three times for every layout. – Evgenii Feb 08 '20 at 12:43
  • Sorry I misunderstood your question, please take a look at this answer [Android Studio Layout Preview Default Theme](https://stackoverflow.com/a/50924060/7948109) – Rahul Gaur Feb 08 '20 at 12:47

1 Answers1

4

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)
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • `@Preview(name = "Dark Mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true)` nor `@Preview(name = "Dark Mode", uiMode = Configuration.UI_MODE_NIGHT_YES` does not show dark background for me – rofrol Jun 29 '22 at 18:47
  • Well, the preview may behave inconsistently; also difficult to debug. Clear caches maybe? – Martin Zeitler Jun 29 '22 at 19:12
  • I have done `File > Invalidate Caches / Restart` from https://stackoverflow.com/a/30450020/588759 and still no dark background – rofrol Jun 29 '22 at 20:31
  • Are you sure you have a day/night theme selected? One can also set color resources: `modifier = Modifier.background(Color(LocalContext.current.resources.getColor(R.color.some_color)))` – Martin Zeitler Jun 29 '22 at 21:40
  • In Kotlin, the property is called `Boolean MaterialTheme.colors.isLight`. – Martin Zeitler Jun 30 '22 at 03:25
  • Are you talking about `@Preview`? I should not deal with `MaterialTheme.colors.isLight` as I understand it. Look at this https://developer.android.com/jetpack/compose/tutorial and Figure 3. Preview showing both light and dark themed composables. – rofrol Jul 04 '22 at 10:53