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

Commit 35b902d

Browse files
authored
Merge pull request #155 from gcoleman799/custom-dark-theme
Custom dark theme
2 parents da570e4 + b4c6e58 commit 35b902d

File tree

6 files changed

+85
-25
lines changed

6 files changed

+85
-25
lines changed

WebView/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
android:label="@string/app_name"
2626
android:roundIcon="@mipmap/ic_launcher_round"
2727
android:supportsRtl="true"
28-
android:theme="@style/AppTheme">
28+
android:theme="@style/Theme.MyApp">
2929
<activity android:name=".MainActivity">
3030
<intent-filter>
3131
<action android:name="android.intent.action.MAIN" />

WebView/app/src/main/assets/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
<!DOCTYPE html>
1717
<html>
1818
<head>
19-
<link href="app.css" rel="stylesheet">
2019
<script async src="main.js"></script>
20+
<meta name="color-scheme" content="dark light">
21+
<link rel="stylesheet" href="style.css">
2122
</head>
2223
<body onload="getData()">
2324
<label for="location">Choose a location:</label>

WebView/app/src/main/assets/app.css renamed to WebView/app/src/main/assets/style.css

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,40 @@
1414
* limitations under the License.
1515
*/
1616
body {
17-
background-color: light-blue;
17+
background-color: white;
1818
}
19-
h1 {
20-
color: white;
19+
h1, h2 {
2120
text-align: center;
22-
}
23-
h2 {
24-
color: grey;
21+
color: black;
2522
text-align: center;
2623
}
2724
p {
2825
font-family: verdana;
2926
font-size: 20px;
3027
}
3128
label {
32-
font-family: verdana;
33-
font-size: 20px;
29+
font-family: verdana;
30+
font-size: 20px;
3431
}
3532
img {
3633
border-radius: 4px;
3734
padding: 5px;
3835
}
3936
.button {
37+
color: black;
38+
background-color:#03DAC6;
4039
font-family: verdana;
41-
background-color:#44c767;
4240
border-radius:8px;
43-
border:1px solid #18ab29;
41+
border:1px solid #03DAC6;
4442
display:inline-block;
4543
cursor:pointer;
46-
color:#ffffff;
4744
font-size:18px;
4845
padding:16px 31px;
4946
text-decoration:none;
5047
text-shadow:0px 1px 0px #2f6627;
5148
margin-left: 27%;
5249
margin-top: 5%;
5350
}
54-
.button:hover {
55-
background-color:#42b007;
56-
}
5751
.button:active {
5852
position:relative;
5953
top:1px;
@@ -91,4 +85,17 @@ img {
9185
padding-top: 20%;
9286
padding-left: 5%;
9387
padding-right: 5%;
88+
}
89+
@media (prefers-color-scheme: dark) {
90+
body {
91+
background: #383838;
92+
}
93+
h1, h2 {
94+
color: white;
95+
}
96+
select {
97+
color: white;
98+
background-color: #1f1f1f;
99+
border-color: #121212;
100+
}
94101
}

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

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import androidx.core.content.ContextCompat.startActivity
3333
import androidx.webkit.JavaScriptReplyProxy
3434
import androidx.webkit.WebMessageCompat
3535
import androidx.webkit.WebSettingsCompat
36+
import androidx.webkit.WebSettingsCompat.DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING
3637
import androidx.webkit.WebViewAssetLoader
3738
import androidx.webkit.WebViewClientCompat
3839
import androidx.webkit.WebViewCompat
@@ -129,9 +130,16 @@ class MainActivity : AppCompatActivity() {
129130
setContentView(binding.root)
130131
val jsObjName = "jsObject"
131132
val allowedOriginRules = setOf<String>("https://gcoleman799.github.io")
132-
133-
// Check if the system is set to light or dark mode
133+
134+
// Configuring Dark Theme
135+
// *NOTE* : The force dark setting is not persistent. You must call the static
136+
// method every time your app process is started.
137+
// *NOTE* : The change from day<->night mode is a
138+
// configuration change so by default the activity will be restarted
139+
// (and pickup the new values to apply the theme). Take care when overriding this
140+
// default behavior to ensure this method is still called when changes are made.
134141
val nightModeFlag = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
142+
// Check if the system is set to light or dark mode
135143
if (nightModeFlag == Configuration.UI_MODE_NIGHT_YES) {
136144
// Switch WebView to dark mode; uses default dark theme
137145
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
@@ -140,11 +148,28 @@ class MainActivity : AppCompatActivity() {
140148
WebSettingsCompat.FORCE_DARK_ON
141149
)
142150
}
143-
// TODO: set darkening strategy and use custom CSS for dark theme
151+
152+
/* Set how WebView content should be darkened. There are three options for how to darken
153+
* a WebView.
154+
* PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING- checks for the "color-scheme" <meta> tag.
155+
* If present, it uses media queries. If absent, it applies user-agent (automatic) darkening
156+
* DARK_STRATEGY_WEB_THEME_DARKENING_ONLY - uses media queries always, even if there's
157+
* no "color-scheme" <meta> tag present.
158+
* DARK_STRATEGY_USER_AGENT_DARKENING_ONLY - it ignores web page theme and always applies
159+
* user-agent (automatic) darkening.
160+
* More information about Force Dark Strategy can be found here:
161+
* https://developer.android.com/reference/androidx/webkit/WebSettingsCompat#setForceDarkStrategy(android.webkit.WebSettings,%20int)
162+
*/
163+
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK_STRATEGY)) {
164+
WebSettingsCompat.setForceDarkStrategy(
165+
binding.webview.settings,
166+
DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING
167+
)
168+
}
144169
}
145170

146171
// Configure asset loader with custom domain
147-
// * NOTE THAT *:
172+
// *NOTE* :
148173
// The assets path handler is set with the sub path /views-widgets-samples/ here because we are tyring to ensure
149174
// that the address loaded with loadUrl("https://raw.githubusercontent.com/views-widgets-samples/assets/index.html") does
150175
// not conflict with a real web address. In this case, if the path were only /assests/ we would need to load
@@ -153,8 +178,14 @@ class MainActivity : AppCompatActivity() {
153178
// Therefore we must let the AssetLoader know to expect the /views-widgets-samples/ sub path as well as the /assets/.
154179
val assetLoader = WebViewAssetLoader.Builder()
155180
.setDomain("raw.githubusercontent.com")
156-
.addPathHandler("/views-widgets-samples/assets/", WebViewAssetLoader.AssetsPathHandler(this))
157-
.addPathHandler("/views-widgets-samples/res/", WebViewAssetLoader.ResourcesPathHandler(this))
181+
.addPathHandler(
182+
"/views-widgets-samples/assets/",
183+
WebViewAssetLoader.AssetsPathHandler(this)
184+
)
185+
.addPathHandler(
186+
"/views-widgets-samples/res/",
187+
WebViewAssetLoader.ResourcesPathHandler(this)
188+
)
158189
.build()
159190

160191
// Set clients
@@ -182,6 +213,5 @@ class MainActivity : AppCompatActivity() {
182213
) { message -> invokeShareIntent(message) }
183214

184215
// Load the content
185-
binding.webview.loadUrl("https://raw.githubusercontent.com/views-widgets-samples/assets/index.html")
186-
}
216+
binding.webview.loadUrl("https://raw.githubusercontent.com/views-widgets-samples/assets/index.html") }
187217
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2020 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<resources>
19+
<color name="colorPrimary">#3700B3</color>
20+
<color name="colorPrimaryDark">#BB86FC</color>
21+
<color name="colorAccent">#03DAC5</color>
22+
</resources>

WebView/app/src/main/res/values/styles.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<resources>
1818

1919
<!-- Base application theme. -->
20-
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
20+
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
2121
<!-- Customize your theme here. -->
2222
<item name="colorPrimary">@color/colorPrimary</item>
2323
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>

0 commit comments

Comments
 (0)