티스토리 뷰

android

how to use web view in android

boywin1992 2022. 5. 21. 17:44
728x90

1. MainActivity.kt

package com.tistory.boywin1992

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.WebSettings
import android.webkit.WebView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val webView = findViewById<WebView>(R.id.webView)
        val url = "https://boywin1992.tistory.com/"

        // zoom
        webView.settings.setSupportZoom(true)
        webView.settings.builtInZoomControls = true
        webView.settings.displayZoomControls = true
        // 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. CustomWebViewClient

package com.tistory.boywin1992

import android.app.Activity
import android.view.Gravity
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.FrameLayout
import android.widget.TextView
import com.google.android.material.snackbar.Snackbar

class CustomWebViewClient(private val activity : Activity): WebViewClient() {
    override fun onPageFinished(view: WebView?, url: String?) {
        super.onPageFinished(view, url)

        val activityView = activity.window.decorView.rootView // fragment
        val message = "hello snack bar\nurl = $url"
        val snackBar = Snackbar.make(activityView, message, Snackbar.LENGTH_SHORT)
        val snackBarView = snackBar.view
        val params = snackBarView.layoutParams as FrameLayout.LayoutParams
        params.gravity = Gravity.CENTER
        snackBarView.layoutParams = params
        snackBarView.findViewById<TextView>(R.id.snackbar_text).maxLines = 10
        snackBar.show()
    }
}

 

3. CustomWebChromeClient

package com.tistory.boywin1992

import android.app.Activity
import android.app.Dialog
import android.os.Message
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView

class CustomWebChromeClient(private val activity: Activity,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)
        // test
//            webView.webView.loadUrl("file:///android_asset/sample.html")
        //

        return super.onCreateWindow(view, isDialog, isUserGesture, resultMsg)
    }
}

 

4. AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tistory.boywin1992">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.boywin1992"
        android:usesCleartextTraffic="true">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

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
글 보관함