Enhanced WebView component for Android that works as intended out of the box, featuring a modern, chainable Fluent API.
- Predictive Back Support: Fully compatible with Android 14+ system back gestures via
.setupPredictiveBack(). - Domain Whitelisting: Lock your WebView to specific domains for better security.
- Fragment Ready: Fixed context issues so fullscreen video and permissions work perfectly inside Fragments.
- Architecture Upgrade: Resolved Gradle cache and manifest merger conflicts.
- Android 5.0+ (API level 21+)
- AndroidX enabled project
- Add this library to your project
-
Declare the Gradle repository in your root
settings.gradle.ktsdependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() maven { url = uri("[https://jitpack.io](https://jitpack.io)") } } } -
Declare the Gradle dependency in your app module's
build.gradle.ktsdependencies { implementation 'com.github.owaisraza10:CompleteWebView:1.0.0' }
-
<uses-permission android:name="android.permission.INTERNET" />- XML
<com.owaisraza.completewebview.CompleteWebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
- Kotlin
class MainActivity : AppCompatActivity() {
private lateinit var mWebView: CompleteWebView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mWebView = findViewById(R.id.webview)
// Setup the File Picker (Required if you want to support <input type="file">)
val filePickerLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
mWebView.handleFileChosen(result.data?.data)
}
mWebView.onFileChooserRequested = { intent ->
if (intent != null) filePickerLauncher.launch(intent)
}
// Configure and load in one clean chain
mWebView.loadUrlString("[https://www.example.org/](https://www.example.org/)")
.enableJavaScript(true)
.enableCookies(true)
.enablePullToRefresh(true)
.enableSmartDarkMode(true)
// Setup the back button to handle browser history and fullscreen video
onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (!mWebView.handleBackPress()) {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
}
}
})
}
}
-
Fullscreen HTML5 video support (YouTube, Vimeo, etc.) is handled automatically.
-
Deep linking to native apps (YouTube, WhatsApp, Twitter, mailto:) is handled automatically.
-
Offline custom error pages are handled automatically (No more Android dinosaur screens).
-
Features are configured via a chainable Fluent API.
-
Receive callbacks when pages start/finish loading, or when file downloads begin.
mWebView.onPageStarted = { url ->
// a new page started loading
}
mWebView.onPageFinished = { url ->
// the new page finished loading
}
mWebView.onDownloadStarted = { fileName ->
// the Android DownloadManager has started downloading a file
}
- Kotlin
mWebView.enablePullToRefresh(true)
-
(Requires AndroidX Webkit support)
-
Kotlin
mWebView.enableSmartDarkMode(true)
- Kotlin
mWebView.enableAdBlock(true)
Bypass the 403: disallowed_useragent error to allow "Sign in with Google" prompts inside the WebView
- Kotlin
mWebView.supportGoogleLoginBypass(true)
- Kotlin
mWebView.enableGeolocation(true)
- Kotlin
mWebView.enableCameraSupport(true)
- Kotlin
mWebView.addHttpHeader("Authorization", "Bearer token123")
- Kotlin
mWebView.addPermittedHostname("example.org")
mWebView.onExternalPageRequest = { url ->
// the user tried to open a page from a non-permitted hostname
}
- Kotlin
mWebView.preventCaching(true)
- Kotlin
mWebView.enableCookies(false)
- Kotlin
mWebView.setMixedContentAllowed(true)
mWebView.setMixedContentAllowed(false)
- Kotlin
mWebView.setDesktopMode(true)
mWebView.setDesktopMode(false)
- Kotlin
mWebView.loadHtml("<html>...</html>")
val myBaseUrl = "[http://www.example.com/](http://www.example.com/)"
mWebView.loadHtml("<html>...</html>", myBaseUrl)
- Kotlin
- Expose Kotlin to JS
mWebView.enableJsBridge("AndroidBridge")
mWebView.onWebMessageReceived = { message ->
// Received a message from Javascript
}
- Send data from Kotlin to a JS function
mWebView.sendDataToWeb("myJavascriptFunction", "{'key': 'value'}")
All contributions are welcome! If you wish to contribute, please create an issue first so that your feature, problem or question can be discussed.
This project is licensed under the terms of the MIT License.