Android WebView in Kotlin

WebView is a view that is used to display web pages inside the app. It is used to turn the app into a web application. In this article let’s display the https://www.geeksforgeeks.org/ inside the Android Application using Kotlin.

Note: To implement Android WebView in Java please refer How to use WebView in Android using Java.

Class Hierarchy

kotlin.Any
     android.view.View
          android.view.ViewGroup
               android.widget.AbsoluteLayout
                    android.webkit.WebView

Approach

Step 1: Create a new project



To create a new project in android studio please refer, how to Create/Start a New Project in Android Studio.

Step 2: Modify activity_main.xml

This is used to set the front end of the Application, we place the WebView in the front end.

XML

filter_none

edit
close

play_arrow

link
brightness_4
code

<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!-- Place Web-view on the Screen -->
    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></WebView>
  
</androidx.constraintlayout.widget.ConstraintLayout>

chevron_right


Step 3: Modify MainActivity.kt

This is the back end of the Application, here we assign values to the views and actions to the views.

Kotlin

filter_none

edit
close

play_arrow

link
brightness_4
code

package com.example.webview_kotlin
  
import android.os.Bundle
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
  
class MainActivity : AppCompatActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // WebViewClient allows you to handle
        // onPageFinished and override Url loading.
        webView.webViewClient = WebViewClient()
  
        // this will load the url of the website
        webView.loadUrl("https://www.geeksforgeeks.org/")
  
        // this will enable the javascript settings
        webView.settings.javaScriptEnabled = true
  
        // if you want to enable zoom feature
        webView.settings.setSupportZoom(true)
    }
  
    // if you press Back button this code will work
    override fun onBackPressed() {
        // if your webview can go back it will go back
        if (webView.canGoBack())
            webView.goBack()
        // if your webview cannot go back
        // it will exit the application
        else
            super.onBackPressed()
    }
}

chevron_right


Step 4: Modify AndroidManifest.xml

In AndroidManifest.xml, one needs to include the below permission, in order to access the internet.

XML

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
  
   package="com.example.webview_kotlin">
  
    <!-- Give permission for app to acess the Internet -->
    <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/AppTheme">
       <activity android:name=".MainActivity">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
   </application>
</manifest>

chevron_right



Output: Run on Emulator

When we run the application either on the Emulator or on the Android Smartphone we can see this as our output. Remember to turn on the internet on your device. 

output screen




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.