Android WebView Kotlin

15-Sep-2024

Embedded web content in your Android Studio Project with the WebView widget in Kotlin


Here is the tutorial for Java



A simple example of using WebView in an Android application:


There Are Some Common Attributes for WebView :

id
This is a unique identity.
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size



Step 1:

File open res/layout/activity_main.xml file −


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>

Step2 :

Make sure to add the necessary permissions in the AndroidManifest.xml file if your web page requires internet access. For example:

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


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

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


<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.NewTestProject"
tools:targetApi="31">
<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>


Step3 :

File open com.microappvalley.newtestproject/MainActivity.kt file −:


Kotlin:


package com.microappvalley.newtestproject

import android.annotation.SuppressLint
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Button
import android.widget.ImageView
import android.widget.Switch
import android.widget.Toast

class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Find the WebView by its id
webView = findViewById(R.id.webView)

// Enable JavaScript (optional, depending on your requirements)
webView.settings.javaScriptEnabled = true

// Set a WebViewClient to handle page navigation
webView.webViewClient = WebViewClient()

// Set a WebChromeClient to handle progress updates (optional)
webView.webChromeClient = WebChromeClient()

// Load a URL into the WebView
webView.loadUrl("https://www.google.com")


}
}

Open Device Manager, run the emulator, and  then run the application. Next, check the working output and check the output  you declared in your code.


Output:


Comments