Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dark theme android app

I'm trying to create a dark theme similar to the one in OxygenOS on OnePlus devices.

enter image description here

I changed the window background to black but the problem is the action bar is not becoming pure black.

<style name="DarkTheme" parent="Theme.AppCompact">
    <item name="android:colorPrimary">@color/black</item>
    <item name="android:colorPrimaryDark">@color/black</item>
    <item name="android:textColorPrimary">@color/white</item>
    <item name="android:colorAccent">@color/white</item>
    <item name="android:color">@color/white</item>
    <item name="android:windowBackground">@color/black</item>
    <item name="android:navigationBarColor">@color/black</item>
    <item name="android:actionBarStyle">@color/black</item>
</style>
like image 630
Mark Ben Avatar asked Jan 22 '16 23:01

Mark Ben


People also ask

Is dark theme coming to Android apps?

This year at I/O 2019 Google announced that Android will now support Dark Theme at OS level. At the same time we’re seeing more Google apps being rolled out with dark UI support. This will become soon a common pattern and other apps will follow on this path.

What are the best apps for dark mode on Android?

Gboard also offers theme support, with one of these themes being a proper black theme. And with keyboards being a fixture in almost every app, this is definitely one of the first dark mode apps you should grab. This app has long been a fixture on Android smartphones, and for very good reason.

What is the dark theme for Kindle?

The dark theme for Kindle only changes the layout of the app UI. If you want to read the books in the dark mode then you can simply change the background to black. It helps the eyes during night time and saves battery.

How do dark-themed Android apps work on Android 10?

Note that dark-themed android:windowBackground drawables only work on Android 10. When the app’s theme changes (either through the system setting or AppCompat) it triggers a uiMode configuration change. This means that Activities will be automatically recreated.


1 Answers

SIMPLEST SOLUTION

You can enable/disable dark theme just by:

  1. enable dark theme:

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
    
  2. forcefully disable dark theme:

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
    
  3. set app theme based on mobile settings of dark mode, i.e. if dark mode is enabled then the theme will be set to a dark theme, if not then the default theme, but this will only work in version >= Android version Q

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
    

Notes:

  1. Your base theme for app/activity should be

"Theme.AppCompat.DayNight"

like

<style name="DarkTheme" parent="Theme.AppCompat.DayNight">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
  1. Your res folder's names would end with -night so that different colors and images you can set for day and night themes like

drawable & drawable-night,
values & values-night

like image 124
Kishan Solanki Avatar answered Nov 06 '22 13:11

Kishan Solanki