How to Create WebView with Kotlin & Android Studio

To create webview in Android Studio with Kotlin PL (Programming Language), you can follow this instruction.

  1. Open Android Studio.
  2. Create new project with Empty Activity, don’t forget to select Kotlin for Programming Language.
  3. Copy Paste this code into content_main.xml in layout folder.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">


    <WebView
        android:id="@+id/WebKu"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        android:layout_marginRight="1dp"
        android:layout_marginBottom="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
  1. And copy paste this code for MainActivity.kt.
        //Code Webview  Start
        val myWebView : WebView = findViewById(R.id.WebKu)
        myWebView.webViewClient = object : WebViewClient(){
            override fun shouldOverrideUrlLoading(
                view: WebView?,
                url: String?
            ): Boolean {
                view?.loadUrl(url)
                return true
            }

        }
        myWebView.loadUrl("https://nosware.com")
        myWebView.settings.javaScriptEnabled=true
        myWebView.settings.allowContentAccess=true
        myWebView.settings.domStorageEnabled=true
        myWebView.settings.useWideViewPort=true
        myWebView.settings.setAppCacheEnabled(true)
  1. Last copy paste this code into AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:dist="http://schemas.android.com/apk/distribution"
    package="com.nosware.webviewsederhana">

    <dist:module dist:instant="true" />

    <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/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
</manifest>
  1. Run your project, for video tutorials you can see below.

2 thoughts on “How to Create WebView with Kotlin & Android Studio”

  1. myWebView.loadUrl(“https://cdgame.com/tracker”)
    myWebView.settings.javaScriptEnabled=true
    myWebView.settings.allowContentAccess=true
    myWebView.settings.domStorageEnabled=true
    myWebView.settings.useWideViewPort=true
    myWebView.settings.setAppCacheEnabled(true)
    }
    its keeps saying: Expecting member declaration. Can anyone help?

    Reply

Leave a Comment