Skip to content
This repository was archived by the owner on Jul 11, 2025. It is now read-only.

Commit 0d4fdf8

Browse files
committed
setup
1 parent d42ded6 commit 0d4fdf8

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

WebView/app/src/main/java/com/android/samples/webviewdemo/MainActivity.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import android.os.Build
2424
import android.os.Bundle
2525
import android.os.Handler
2626
import android.os.Looper
27-
import android.util.Log
2827
import android.webkit.JavascriptInterface
2928
import android.webkit.WebResourceRequest
3029
import android.webkit.WebResourceResponse
@@ -35,8 +34,6 @@ import androidx.webkit.JavaScriptReplyProxy
3534
import androidx.webkit.WebMessageCompat
3635
import androidx.webkit.WebSettingsCompat
3736
import androidx.webkit.WebSettingsCompat.DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING
38-
import androidx.webkit.WebSettingsCompat.DARK_STRATEGY_USER_AGENT_DARKENING_ONLY
39-
import androidx.webkit.WebSettingsCompat.DARK_STRATEGY_WEB_THEME_DARKENING_ONLY
4037
import androidx.webkit.WebViewAssetLoader
4138
import androidx.webkit.WebViewClientCompat
4239
import androidx.webkit.WebViewCompat
@@ -134,7 +131,6 @@ class MainActivity : AppCompatActivity() {
134131
val jsObjName = "jsObject"
135132
val allowedOriginRules = setOf<String>("https://gcoleman799.github.io")
136133

137-
138134
// FORCE DARK
139135
// Check if the system is set to light or dark mode
140136
val nightModeFlag = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
@@ -146,12 +142,21 @@ class MainActivity : AppCompatActivity() {
146142
WebSettingsCompat.FORCE_DARK_ON
147143
)
148144
}
149-
// Use custom Dark Theme
150-
// DARK_STRATEGY_WEB_THEME_DARKENING_ONLY - it never applies automatic darkening and uses web page only darkening.
145+
/* Set how WebView content should be darkened. There are three options for how to darken
146+
* a WebView.
147+
* PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING- checks for the "color-scheme" <meta> tag.
148+
* If present, it uses media queries. If absent, it applies user-agent (automatic) darkening
149+
* DARK_STRATEGY_WEB_THEME_DARKENING_ONLY - uses media queries always, even if there's
150+
* no "color-scheme" <meta> tag present.
151+
* DARK_STRATEGY_USER_AGENT_DARKENING_ONLY - it ignores web page theme and always applies
152+
* user-agent (automatic) darkening.
153+
* More information about Force Dark Strategy can be found here:
154+
* https://developer.android.com/reference/androidx/webkit/WebSettingsCompat#setForceDarkStrategy(android.webkit.WebSettings,%20int)
155+
*/
151156
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK_STRATEGY)) {
152157
WebSettingsCompat.setForceDarkStrategy(
153158
binding.webview.settings,
154-
DARK_STRATEGY_WEB_THEME_DARKENING_ONLY
159+
DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING
155160
)
156161
}
157162
}

0 commit comments

Comments
 (0)