android web view settings
1. MainActivity.kt
// web view
webView = findViewById(R.id.webView)
val url = "http://surogong.man-deum.com/pages/customer/load.php"
// new page
webView.settings.javaScriptCanOpenWindowsAutomatically = false
webView.settings.setSupportMultipleWindows(false)
// html size
webView.settings.useWideViewPort = true
webView.settings.loadWithOverviewMode = true
// javascript
webView.settings.javaScriptEnabled = true
// load local images
webView.settings.loadsImagesAutomatically = true
// cache
webView.settings.cacheMode = WebSettings.LOAD_NO_CACHE
// local storage
webView.settings.domStorageEnabled = true
// allow file access
webView.settings.allowFileAccess = true
// database API
webView.settings.databaseEnabled = true
// media play
webView.settings.mediaPlaybackRequiresUserGesture = false
// allow content access with url
webView.settings.allowContentAccess = true
// location API
webView.settings.setGeolocationEnabled(true)
// client
webView.webViewClient = CustomWebViewClient(this)
webView.webChromeClient = CustomWebChromeClient(this, url)
// load URL
webView.loadUrl(url)
2. CustomWebChromeClient.kt
package com.tistory.boywin1992
import android.app.Dialog
import android.net.Uri
import android.os.Message
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
class CustomWebChromeClient(private val activity: MainActivity, private val url: String) :
WebChromeClient() {
override fun onCreateWindow(
view: WebView?,
isDialog: Boolean,
isUserGesture: Boolean,
resultMsg: Message?
): Boolean {
val newWebView = WebView(activity)
// zoom
newWebView.settings.setSupportZoom(true)
newWebView.settings.builtInZoomControls = true
newWebView.settings.displayZoomControls = true
// new page
newWebView.settings.javaScriptCanOpenWindowsAutomatically = false
newWebView.settings.setSupportMultipleWindows(false)
// html size
newWebView.settings.useWideViewPort = true
newWebView.settings.loadWithOverviewMode = true
// javascript
newWebView.settings.javaScriptEnabled = true
// load local images
newWebView.settings.loadsImagesAutomatically = true
// cache
newWebView.settings.cacheMode = WebSettings.LOAD_NO_CACHE
// local storage
newWebView.settings.domStorageEnabled = true
// allow file access
newWebView.settings.allowFileAccess = true
// database API
newWebView.settings.databaseEnabled = true
// media play
newWebView.settings.mediaPlaybackRequiresUserGesture = false
// allow content access with url
newWebView.settings.allowContentAccess = true
// location API
newWebView.settings.setGeolocationEnabled(true)
// client
val dialog = Dialog(activity)
dialog.setContentView(newWebView)
dialog.show()
newWebView.webViewClient = CustomWebViewClient(activity)
newWebView.webChromeClient = object : WebChromeClient() {
override fun onCloseWindow(window: WebView?) {
dialog.dismiss()
}
}
(resultMsg?.obj as WebView.WebViewTransport).webView = newWebView
resultMsg.sendToTarget()
// load URL
newWebView.loadUrl(url)
return super.onCreateWindow(view, isDialog, isUserGesture, resultMsg)
}
}