Time to have a DARK Side
This article has nothing to do with a philosophical approach to have some kind of a dark side of yourself. It is just about Android development, which is a serious business.
There are multiple ways to implement a custom dark theme in Android. (what a surprise(!) ) The one that I am going to explain is the one that looks the cleanest of them all to me. Of course, different approaches would be useful for different needs. So have a look at this cool google guide.
You need an alternative resource file for colors just to be used in dark times. It is like having a localization file for different languages. Here is how;
- Right-click on your ‘res’ folder and select ‘New -> Android Resource File’
- Put ‘colors’ as your file name (I know you already have a colors file.)
- Select ‘Night Mode’ from “Available Qualifiers” list
- Click on the ‘>>’ button
- Select ‘Night mode’ as ‘Night’ from the spinner.
- Click “OK”
That’s it. You have the resource file to override whatever color you want to override when the user device is in the dark mode.
I have two items in my two colors files. They have the same name in two different files. I use those names for the background color and the text color of my text view. As you can see, I also have themes.xml files in values and values-night folders. You may have different whatever resources you want in your values-night folder.
Here are my color items;
values/colors.xml
<color name="backgroundColor">#ffffff</color>
<color name="textColor">#000000</color>
values-night/colors.xml
<color name="backgroundColor">#000000</color>
<color name="textColor">#ffffff</color>
Classic black/white…
This would not be the way to go for you but it is pretty straight forward. You can build a much better structure for your application with the help of the themes and theme attributes. You should definitely check out this talk for further understanding. Feel free to ask or share anything.