Sun. Aug 7th, 2022

Webview digunakan untuk mengakses web page atau halaman web. misal kita punya website kemudian kita masukkan ke dalam aplikasi sehingga ketika user menggunakan aplikasi tersebut otomatis web tersebut akan diakses. hal ini sangat membantu jika user yang tidak tau alamat url kita.

contoh: aplikasi ini akan mengakses web www.insanpembelajar.com

buat desain layout webview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <WebView
        android:id="@+id/webku"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>

kemudian code kan layout webview dengan url web pada MainActivity

package a.riki.cobahello

import android.graphics.Bitmap
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import kotlinx.android.synthetic.main.menu1.*


class MainActivity : AppCompatActivity() {

    private val url = "http://insanpembelajar.com/"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.menu1)
        val settings = webku.settings;

        // javascript enable
        settings.javaScriptEnabled = true

        // cache
        settings.setAppCacheEnabled(true)
        settings.cacheMode = WebSettings.LOAD_DEFAULT
        settings.setAppCachePath(cacheDir.path)


        // zoom in/out
        settings.setSupportZoom(true)
        settings.builtInZoomControls = true
        settings.displayZoomControls = true

        settings.blockNetworkImage = false
        settings.loadsImagesAutomatically = true


        // pengaturan lain untuk webview lainnya
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            settings.safeBrowsingEnabled = true  // api 26
        }

        settings.useWideViewPort = true
        settings.loadWithOverviewMode = true
        settings.javaScriptCanOpenWindowsAutomatically = true
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            settings.mediaPlaybackRequiresUserGesture = false
        }

        settings.domStorageEnabled = true
        settings.setSupportMultipleWindows(true)
        settings.loadWithOverviewMode = true
        settings.allowContentAccess = true
        settings.setGeolocationEnabled(true)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            settings.allowUniversalAccessFromFileURLs = true
        }

        settings.allowFileAccess = true

        webku.fitsSystemWindows = true

        webku.setLayerType(View.LAYER_TYPE_HARDWARE, null)

        webku.loadUrl(url)

        webku.webViewClient = object : WebViewClient() {
            override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
            }

            override fun onPageFinished(view: WebView, url: String) {
            }
        }

    }

    override fun onBackPressed() {
        if (webku.canGoBack()) {
            webku.goBack()
        }
    }
}

tambahkan permission internet pada manifest

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