Alcachofra
Alcachofra

Reputation: 165

Android Colors in Dark Mode

Hello fellow developers.

I've developed and Android App using Android Studio, but I only took into account the layout themes for light mode. Here's how it looks:

light mode

So now, when I switch my phone to dark mode, this happens:

dark mode

How can I change the colors that are being displayed in dark mode?

Upvotes: 12

Views: 21470

Answers (2)

nima
nima

Reputation: 552

In res/values/colors
There are two colors
colors.xml
colors.xml(night)


If night does not exist, you can make it as follows:
Right click on the values folder.
Next New. Then Values Resource file
Enter the colors in the file name field
And enter the values-night in the directory name field.


In colors.xml, select the colors that are for the light, such as:

<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryVariant">#303F9F</color>
<color name="colorButton">#303F9F</color>

In colors.xml(night), set the colors for the night(Same name as colors):

<color name="colorPrimary">#212121</color>
<color name="colorPrimaryVariant">#000000</color> 
<color name="colorButton">#10ffffff</color>

Now if in layout, for example:

<Button
     android: layout_width = "wrap_content"
     android: layout_height = "wrap_content"
     android: background = "@color/colorButton" />

It is selected according to what you put in the light and night colors.

Upvotes: 39

Blundell
Blundell

Reputation: 76574

enter image description here

https://developer.android.com/guide/topics/resources/providing-resources

Use the folders:

/values/
/values-night/

It mentions it here as well: https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#config-changes

"night-qualified resources"

Your themes and styles should avoid hard-coded colors or icons intended for use under a light theme. You should use theme attributes (preferred) or night-qualified resources instead.

Upvotes: 6

Related Questions