-
Couldn't load subscription status.
- Fork 282
Add sample for WebView force dark mode #668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6b5cc5d
7545bf4
4eb7d0a
64e87a8
233c6b1
e95dab9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.example.webviewdarkmode | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import android.os.Bundle | ||
| import android.webkit.WebView | ||
| import android.webkit.WebViewClient | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.webkit.WebSettingsCompat | ||
| import androidx.webkit.WebViewFeature | ||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
| @SuppressLint("SetJavaScriptEnabled") | ||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| // Sets the user interface from a layout resource. | ||
| setContentView(R.layout.activity_main) | ||
|
|
||
| // You still get your WebView instance the standard way. | ||
| // [START android_views_notifications_build_basic] | ||
| val webView: WebView = findViewById(R.id.my_webview) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original snippet has the imports. To include the snippets in the region tag, it would be easiest to include them as comments here. Region tags around the real imports above would not be stable because our style formatting tools in Android Studio and Spotless will reorder imports and comments, which would break region tags if you try to use them up there. |
||
|
|
||
| // This is necessary to keep navigation inside your WebView | ||
| webView.webViewClient = WebViewClient() | ||
|
|
||
| // Enable JavaScript | ||
| webView.settings.javaScriptEnabled = true | ||
|
|
||
| // To enable a modern feature, you pass that instance to a Jetpack Webkit helper. | ||
| if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) { | ||
| WebSettingsCompat.setForceDark(webView.settings, WebSettingsCompat.FORCE_DARK_ON) | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This snippet is supposed to match the snippet here: This snippet also adds additional code: Is this additional code expected and required? |
||
| // [END android_views_notifications_build_basic] | ||
|
|
||
| // Load a URL | ||
| webView.loadUrl("https://developer.android.com") | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not add a new module. We should edit the build file for the existing misc module. https://github.com/android/snippets/blob/main/misc/build.gradle.kts |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| plugins { | ||
| alias(libs.plugins.android.application) | ||
| alias(libs.plugins.kotlin.android) | ||
| alias(libs.plugins.kotlin.compose) | ||
| } | ||
|
|
||
| android { | ||
| namespace = "com.example.webviewdarkmode" | ||
| compileSdk = 36 | ||
|
|
||
| defaultConfig { | ||
| applicationId = "com.example.webviewdarkmode" | ||
| minSdk = 24 | ||
| targetSdk = 36 | ||
| versionCode = 1 | ||
| versionName = "1.0" | ||
|
|
||
| testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
| } | ||
|
|
||
| buildTypes { | ||
| release { | ||
| isMinifyEnabled = false | ||
| proguardFiles( | ||
| getDefaultProguardFile("proguard-android-optimize.txt"), | ||
| "proguard-rules.pro" | ||
| ) | ||
| } | ||
| } | ||
| compileOptions { | ||
| sourceCompatibility = JavaVersion.VERSION_11 | ||
| targetCompatibility = JavaVersion.VERSION_11 | ||
| } | ||
| kotlinOptions { | ||
| jvmTarget = "11" | ||
| } | ||
| buildFeatures { | ||
| compose = true | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation("androidx.webkit:webkit:1.11.0") | ||
| implementation("androidx.appcompat:appcompat:1.7.0") | ||
| implementation(libs.androidx.core.ktx) | ||
| implementation(libs.androidx.lifecycle.runtime.ktx) | ||
| implementation(libs.androidx.activity.compose) | ||
| implementation(platform(libs.androidx.compose.bom)) | ||
| implementation(libs.androidx.ui) | ||
| implementation(libs.androidx.ui.graphics) | ||
| implementation(libs.androidx.ui.tooling.preview) | ||
| implementation(libs.androidx.material3) | ||
| testImplementation(libs.junit) | ||
| androidTestImplementation(libs.androidx.junit) | ||
| androidTestImplementation(libs.androidx.espresso.core) | ||
| androidTestImplementation(platform(libs.androidx.compose.bom)) | ||
| androidTestImplementation(libs.androidx.ui.test.junit4) | ||
| debugImplementation(libs.androidx.ui.tooling) | ||
| debugImplementation(libs.androidx.ui.test.manifest) | ||
| } |
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not add a new Gradle version catalog. We should edit the existing version catalog. https://github.com/android/snippets/blob/main/gradle/libs.versions.toml |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| [versions] | ||
| agp = "8.12.3" | ||
| kotlin = "2.0.21" | ||
| coreKtx = "1.17.0" | ||
| junit = "4.13.2" | ||
| junitVersion = "1.3.0" | ||
| espressoCore = "3.7.0" | ||
| lifecycleRuntimeKtx = "2.9.4" | ||
| activityCompose = "1.11.0" | ||
| composeBom = "2024.09.00" | ||
|
|
||
| [libraries] | ||
| androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } | ||
| junit = { group = "junit", name = "junit", version.ref = "junit" } | ||
| androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" } | ||
| androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" } | ||
| androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" } | ||
| androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" } | ||
| androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" } | ||
| androidx-ui = { group = "androidx.compose.ui", name = "ui" } | ||
| androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" } | ||
| androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" } | ||
| androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" } | ||
| androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" } | ||
| androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" } | ||
| androidx-material3 = { group = "androidx.compose.material3", name = "material3" } | ||
|
|
||
| [plugins] | ||
| android-application = { id = "com.android.application", version.ref = "agp" } | ||
| kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } | ||
| kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } | ||
|
|
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name of this file appears to be a layout but the contents is an Android manifest file. The code snippet uses https://developer.android.com/guide/topics/resources/layout-resource Then the contents will need to be updated to be a layout. For the snippet to compile, it can be a very simple layout file. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
| xmlns:tools="http://schemas.android.com/tools"> | ||
| <uses-permission android:name="android.permission.INTERNET" /> | ||
| <application | ||
| android:allowBackup="true" | ||
| android:dataExtractionRules="@xml/data_extraction_rules" | ||
| android:fullBackupContent="@xml/backup_rules" | ||
| android:icon="@mipmap/ic_launcher" | ||
| android:label="@string/app_name" | ||
| android:roundIcon="@mipmap/ic_launcher_round" | ||
| android:supportsRtl="true" | ||
| android:theme="@style/Theme.Webviewdarkmode"> | ||
| <activity | ||
| android:name=".MainActivity" | ||
| android:exported="true" | ||
| android:label="@string/app_name" | ||
| android:theme="@style/Theme.Webviewdarkmode"> | ||
| <intent-filter> | ||
| <action android:name="android.intent.action.MAIN" /> | ||
|
|
||
| <category android:name="android.intent.category.LAUNCHER" /> | ||
| </intent-filter> | ||
| </activity> | ||
| </application> | ||
|
|
||
| </manifest> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The package should match the directory structure after /java. In this case, it should be
com.example.snippets.jetpackwebkit