Skip to main content

Dark Theme (Dark Mode) in Android WebViews, WKWebViews and CSS



So your apps just implemented a shiny new dark theme and it’s looking 👌

There are lots of benefits to having a dark theme in your application, and having it consistent throughout your application allows for a great user experience. But what happens when the the user runs into a WebView in your app?

Support:
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) { ... }

Set:
WebSettingsCompat.setForceDark(webView.settings, WebSettingsCompat.FORCE_DARK_ON)

Current setting:
val forceDarkMode = WebSettingsCompat.getForceDark(webView.settings)

Joe Birch

Assuming your question is asking how to change the colors of the HTML content you are displaying in a WKWebView based on whether light or dark mode is in effect, there is nothing you do in your app's code. All changes need to be in the CSS being used by your HTML content.

CSS dark mode via :root variables, explicit colors and @media query:
:root {
    color-scheme: light dark;
        --h1-color: #333;
        --header-bg-clr: #FFF1FF;
        --header-txt-clr: white;
    }
    @media (prefers-color-scheme: dark) {
    :root {
        color-scheme: light dark;
        --h1-color: #333;
        --header-bg-clr: #FFF1FF;
        --header-txt-clr: white;
        }
    }

body { }
h1 { color: var(--h1-color); }
.header {
    background-color: var (--header-bg-clr);
    color: var(--header-txt-clr);
}

Automatic dark mode by inverting colors:
@media (prefers-color-scheme: dark) {
     /* root tag inverting all the components color except the table.*/
     : root {
            color-scheme: light dark;
        filter: invert(100%);
       -webkit-filter: invert(100%)
     }
    /*table tag needed for inverting table content.*/
    table {
            filter: invert(100%);
     }
    /* If We do color invert in : root , images will be color inverted and it will be in negative. If we invert again these negative images, it will be positive.*/
     img {
         filter: invert(100%);
     }

    }

StackOverflow

Comments

Most Favorite Posts

CFPropertyList

The PHP implementation of Apple's PropertyList plist can handle XML PropertyLists as well as binary PropertyLists. It offers functionality to easily convert data between worlds, e.g. recalculating timestamps from unix epoch to apple epoch and vice versa. A feature to automagically create (guess) the plist structure from a normal PHP data structure will help you dump your data to plist in no time. github

Firebase App Indexing from Google for Android and iOS

Firebase App Indexing gets your app into Google Search. If users have your app installed, they can launch your app and go directly to the content they're searching for. App Indexing reengages your app users by helping them find both public and personal content right on their device, even offering query autocompletions to help them more quickly find what they need. If users don’t yet have your app, relevant queries trigger an install card for your app in Search results. App Indexing lets Google index your app just as if it were a website. For users with your app installed, deep links to your app - on Android or iOS - appear in Google Search results, allowing users to find exactly the right content within your app. In addition to driving re-engagement, App Indexing on Android will also surface install buttons for users who do not yet have your app installed. Since 1 in 4 appsare already being discovered through search, App Indexing is a simple and free method for acquiring new u...

App Indexing

A better search experience for apps and users with linking to in-app content. Google is working with app developers and webmasters to index the content of apps and relate them to websites. When relevant, Google Search results on Android will include deep links to apps. App Indexing

Judo App - Server Driven UI out of the box

Judo App Judo brings server-driven UI to your iOS and Android apps. Build user interfaces visually in a fraction of time and publish them instantly without submitting to the app store. Build Experiences - With No Code The Judo app for macOS, available through the App Store, is built for design professionals with common keyboard shortcuts and familiar concepts like canvas, layers and inspector panel. Workflow is streamlined with the ability to drag and drop media files directly into your experiences and manage your own Judo files in Finder. Manage Creative Execution A Judo experience is interactive and can include text, images, video and buttons. An experience may be part of a screen, a single screen, or more typically multiple linked screens. Judo supports screen transitions, carousels, horizontal scrolling and modals. Clients can add custom fonts and define global colors and these are updates applied universally. Effortlessly Deploy Judo Cloud syncs your experiences with your iOS and ...

Team building: "Meme-Your-Colleague"

Lately we were hiring a lot of developers and had to integrate them in our team. So we decided to do an infotainment team event, targeting both: getting to know each other on a personal level and learning about the business and technical state, challenges and visions. Usually a meeting like this starts with everybody telling a few words about themselves... one... after... each... other... and after third speaker latest everybody gets bored. So we came up with the "Meme-Your-Colleague" game: Build teams of two persons Give them half an hour to get to know each other Both have to assemble a five minute presentation of five slides of each other Each slide consists of exactly one meme telling a (fun) fact of that person You may want to use a meme generator  and a presentation tool like Keynote or PowerPoint. It ended up with the whole team having a lot of fun: a perfect ice breaker! Give it a shot!

In-App Purchase Receipt Validation on iOS

A vulnerability has been discovered in iOS 5.1 and earlier related to validating in-app purchase receipts by connecting to the App Store server directly from an iOS device. An attacker can alter the DNS table to redirect these requests to a server controlled by the attacker. Using a certificate authority controlled by the attacker and installed on the device by the user, the attacker can issue a SSL certificate that fraudulently identifies the attacker’s server as an App Store server. When this fraudulent server is asked to validate an invalid receipt, it responds as if the receipt were valid. iOS 6 will address this vulnerability. If your app follows the best practices described below then it is not affected by this attack. iOS Developer Library In-App-Klau jetzt auch im Mac App Store ( Heise )

How to link to TestFlight App in iOS

There are two things you need to do. First, check to see if TestFlight is installed. Then create a new link to your app. NSURL *customAppURL = [NSURL URLWithString:@"itms-beta://"]; if ([[UIApplication sharedApplication] canOpenURL:customAppURL]) {     // TestFlight is installed     // Special link that includes the app's Apple ID     customAppURL = [NSURL URLWithString:@"https://beta.itunes.apple.com/v1/app/978489855"];      [[UIApplication sharedApplication] openURL:customAppURL]; } This special https://beta.itunes.apple.com URL will be opened directly in TestFlight. Finally, if you are using iOS 9 (or later), you need to make an addition to your Info.plist to get the canOpenURL: method to work. If your app is linked on or after iOS 9.0, you must declare the URL schemes you want to pass to this method. Do this by using the LSApplicationQueriesSchemes array in your Xcode project’s Info.plist file. For each URL scheme you wan...