Intermediate: Changing App theme using Huawei Dark mode Awareness Service

RChancha
Huawei Developers
Published in
3 min readMay 25, 2021

--

Introduction

In this article, we will use HMS Dark mode awareness to support Dark theme in our app.

As more and more devices supporting dark theme is getting released every month, so Android app need to support dark mode is on the rise. And if you have not already updated your app to support Dark theme, your app could soon appear to be an outdated application.

Development Overview

Prerequisite

1. Must have a Huawei Developer Account.

2. Must have Android Studio 3.0 or later.

3. Must have Huawei phone running EMUI 5.0 with HMS core version 4.0.2.300.

Software Requirements

1. Java SDK 1.7 or later.

2. Android 5.0 or later.

Preparation

1. Create an app or project in the Huawei AppGallery Connect.

2. Provide the SHA Key and App Package name of the project in App Information Section and enable the Awareness Kit API.

3. Download the agconnect-services.json file.

4. Create an Android project.

Integration

  1. Add below to build.gradle (project) file under buildscript/repositories and allprojects/repositories.

2. Add below to build.gradle (app) file, under dependencies to use the Awareness kit SDK.

Development

We will create a simple UI design which will be shown when dark mode is off.

For dark theme, we will define a color.xml inside qualifier values-night in res folder.

Now let us define style.xml in res > values directory to support dark theme.

We will create drawable-night-xxx directory and add dark theme images into it.

To check if dark mode is on/off, we need to obtain Capture Client instance of Awareness kit. Using Capture Client instance, we will call the dark mode status query.

Based on returned dark mode status, we are setting our app theme.

Code snippet of MainActivity.java

Result

Tips and Tricks

1. Minimum SDK version should be 29.

2. To enable dark mode, choose Settings > Display & Brightness and enable dark mode.

3. Dark mode awareness capability will not support for barrier function.

Conclusion

In this article, we have learnt how easily can change our application theme using HMS dark mode detection. We can check programmatically if dark mode is enabled or not and based on the dark mode awareness query result, we can load our theme.

Hope you found this story useful and interesting.

Happy coding! 😃 💻

References

Dark Mode Awareness

--

--