Android Dark Mode

Android Dark Mode

msapps | January 21 / 2021 | Android

Dark theme is available in Android 10 (API level 29) and higher

To Enable Dark Mode search in the notification tray or open Device system Settings -> Display -> Theme

Benefits:

  1. Saves Battery power.
  2. easy to see in low-light environments.
  3. help dealing with sensitive eyes.

Develop Dark theme enable app

make your manifest Application use theme that inherit from

Theme.AppCompat.DayNight
or
Theme.MaterialComponents.DayNight

 

// in manifest add – configChanges=”uiMode”

 

<application

 

        android:icon=”@mipmap/ic_launcher”

 

        android:label=”@string/app_name”

 

        android:roundIcon=”@mipmap/ic_launcher_round”

 

        android:theme=”@style/Theme.StyleSample”>

 

        <activity

 

            android:name=”.MainActivity”

 

            android:configChanges=”uiMode”

 

            >

 

  
 

// override in Activity

  
 

    override fun onConfigurationChanged(newConfig: Configuration) {

 

        when (newConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK) {

 

            Configuration.UI_MODE_NIGHT_NO -> {

 

            }

 

            Configuration.UI_MODE_NIGHT_YES -> {

 

                darkMode = true

 

            }

 

        }

 

        super.onConfigurationChanged(newConfig);

 

    }

view rawgistfile1.txt hosted with ❤ by GitHub

Colors

  1. You will need “values-night” folder in res folder with themes.xml & colors.xml
  2. You can have also drawable-night folder!
  3. You can try to avoid hard coded colors.
  4. Need to use precise hex color, you can have two colors.xml files one in “value” folder and one in “values-night” folder.

For example you can have a textview which has textColor that has been defined in that 2 colors files under the same key, i each file the color value is deferent and theme suitable.

Icons

Beside color changes – the designer should Go over All App Icons And adjust them to the night theme as well.
the designer can demand using set of icons for night theme.
you can use drawable-night folder or the code below to recognise the current theme

 

    fun isDarkMode():Boolean{

 

        val currentNightMode = (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK)

 

        if (currentNightMode == Configuration.UI_MODE_NIGHT_YES) return true

 

        return false

 

    }

view rawis Dark Mode hosted with ❤ by GitHub

‎‎

Button Click To force Dark mode to your app

 

       //All App

 

       findViewById<Button>(R.id.dark).setOnClickListener {

 

            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

 

            if (Build.VERSION.SDK_INT >= 11) {

 

                recreate();

 

            }

 

        }

 

        //Local

 

        findViewById<Button>(R.id.dark).setOnClickListener {

 

            delegate.localNightMode = AppCompatDelegate.MODE_NIGHT_YES;

 

            if (Build.VERSION.SDK_INT >= 11) {

 

                recreate();

 

            }

 

        }

  

view rawgistfile1.txt hosted with ❤ by GitHub

‎‎

When a user change the the device theme All Apps Activities will get recreated
if you want to get notify you should follow code below

 

// in manifest add – configChanges=”uiMode”

 

<application

 

        android:icon=”@mipmap/ic_launcher”

 

        android:label=”@string/app_name”

 

        android:roundIcon=”@mipmap/ic_launcher_round”

 

        android:theme=”@style/Theme.StyleSample”>

 

        <activity

 

            android:name=”.MainActivity”

 

            android:configChanges=”uiMode”

 

            >

 

  
 

// override in Activity

  
 

    override fun onConfigurationChanged(newConfig: Configuration) {

 

        when (newConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK) {

 

            Configuration.UI_MODE_NIGHT_NO -> {

 

            }

 

            Configuration.UI_MODE_NIGHT_YES -> {

 

                darkMode = true

 

            }

 

        }

 

        super.onConfigurationChanged(newConfig);

 

    }

view rawgistfile1.txthosted with ❤ by GitHub

Don’t forget to test notifications and launcher Icon design.

As been mention when user start using dark mode the Activity will get recreated – so test this for some saving state bugs.

Share

More posts

Augmented Reality iOS

Augmented Reality iOS

Daniel Radshun | December 01 / 2019 | iOS Augmented reality makes user experience objects appearance in 2D or 3D in

Android Instant App

Android Instant App

msapps | January 21 / 2018 | Uncategorized Have you ever thought about the idea to make people use your app

Send us a message