티스토리 뷰

android

android web view settings

boywin1992 2022. 5. 23. 20:03
728x90

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)
    }
}

728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함