How to Use WebView in Android?

WebView is a view that displays web pages inside the application. It is used to turn the application into a web application.

public class WebView extends AbsoluteLayout implements ViewTreeObserver.OnGlobalFocusChangeListener, ViewGroup.OnHierarchyChangeListener

Class Hierarchy:

java.lang.Object ↳ android.view.View ↳ android.view.ViewGroup ↳ android.widget.AbsoluteLayout ↳ android.webkit.WebView

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.

Step 2: Working with the MainActivity File

Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.

Java

import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity < protected void onCreate(Bundle savedInstanceState) < super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Find the WebView by its unique ID WebView webView = findViewById(R.id.web); // loading https://www.geeksforgeeks.org url in the WebView. webView.loadUrl( "https://www.geeksforgeeks.org" ); // this will enable the javascript. webView.getSettings().setJavaScriptEnabled( true ); // WebViewClient allows you to handle // onPageFinished and override Url loading. webView.setWebViewClient( new WebViewClient());

Kotlin

import android.os.Bundle import android.webkit.WebView import android.webkit.WebViewClient import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Find the WebView by its unique ID val webView = findViewById(R.id.web) // loading http://www.google.com url in the WebView. webView.loadUrl( "https://www.geeksforgeeks.org" ) // this will enable the javascript. webView.settings.javaScriptEnabled = true // WebViewClient allows you to handle // onPageFinished and override Url loading. webView.webViewClient = WebViewClient()

Step 3: Working with the XML Files

Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

XML

< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = "com.gfg.android.examplekotlin.MainActivity" > android:id = "@+id/web" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:layout_editor_absoluteX = "8dp" tools:layout_editor_absoluteY = "8dp" />

Step 4: Adding Permissions to the AndroidManifest.xml File

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

Output:

WebView in Android

Like Article -->

Please Login to comment.

Similar Reads

WebView in Android using Jetpack Compose

In Android, a WebView is an extension of View Class that allows us to display webpages. In simpler words, if you want to display a webpage in Activity Layout, then you can implement a WebView to display it. It is very similar to a browser but isn't one. A WebView can rather be referred to as a show or a preview of a browser as it lacks most functio

3 min read 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.

2 min read How to Create Dynamic WebView in Android with Firebase?

Converting a website into an application seems like a basic task to do on Android. With the help of WebView, we can show any webpage in our Android Application. We just have to implement the widget of WebView and add the URL inside the WebView which we have to load. So if you are looking for loading a website into your app which can be changed dyna

6 min read How to Display Progress in ProgressBar While Loading URL in WebView in Android?

ProgressBar in Android is a UI element that displays the status of some work being done in the background in the form of some animation. It can be anything, like computing, loading, searching, fetching, downloading, etc. ProgressBar broadly has two types namely spinning wheel and horizontal bar. In this article, we want to show you how you could im

2 min read How to Build a News App Using WebView Controller in Android Studio?

In this article, we are going to make a News App with help of a WebView Controller in Android Studio. By making this application we will learn how to access Internet Permission in our android application and how to use WebView with its class named WebView Controller. After making this application you will also be aware of Navigation Drawer activity

7 min read Android WebView with JavaScript Interface

JavascriptInterface Annotation provides the facility to create any method as the javascript Interface which means that method can be used by web components to communicate with Android. For example: To store the web-based data in Android Shared Preference, or in a local Database.To show a customized dialog box. Storing the web content in Android Cli

3 min read Convert WebView to PDF in Android with Kotlin

Sometimes the user has to save some article that is being displayed on the web browser. So for saving that article or a blog users can simply save that specific web page in the form of a PDF on their device. We can implement this feature to save the pdf format of the web page which is being displayed in the WebView. In this article, we will take a

5 min read Android Jetpack Compose - Create Dynamic WebView using Firebase Realtime Database

Converting a website into an application seems like a basic task to do on Android. With the help of WebView, we can show any webpage in our Android Application. We just have to implement the widget of WebView and add the URL inside the WebView that we have to load. So if you are looking for loading a website into your app which can be changed dynam

8 min read Android - Search a Text in WebView

WebView allows you to display web content in your activity layout or Fragment but lacks some of the features of fully-developed browsers. A WebView is useful when you need to increase control over the UI and advanced configuration options that will allow you to embed web pages in a specially-designed environment for your app. Note: To know more abo

7 min read How to Integrate Google Forms in Your Android App Without Using WebView?

Sometimes we have to set some type of form to send the data to our backend this can be any type of form, it can be a contact form or some kind of support form but there is no kind of tool that is provided by Android and other tools require Web View or paid to implement itself so in this article we are going to integrate Google Form in our android a

6 min read How to Convert WebView to PDF in Android?

Sometimes there is a need to save some articles available on the internet in the form of PDF files. And to do so there are many ways, one can use any browser extension or any software or any website to do so. But in order to implement this feature in the android app, one can't rely on other software or websites to do so. So to implement this amazin

6 min read JavaFX | WebView Class

WebView class is a part of JavaFX. WebView can create and manage a WebEngine and display its content. The associated WebEngine is created automatically at construction time and cannot be changed. WebView manages keyboard and mouse events and automatically adds a scrollbar to the WebView. Constructor of the class: WebView(): Creates a new web view o

4 min read How to Use Stack Overflow to Fix Errors During Android App Development in Android Studio?

Stack Overflow, the life savior of developers, feels like Stack Overflow is built for the developers, by the developers, of the developers. According to Wikipedia: "Stack Overflow is a question-and-answer site for professional and enthusiast programmers". So being a developer or programmer can you imagine your life without Stack Overflow…. ? Most p

4 min read How to Use Fast Android Networking Library in Android with Example?

In Android, we know multiple networking libraries like Retrofit, Volley. We use these libraries specifically for making networking requests like performing actions on API. But Besides that, there is another library called Fast Networking Library which has a few advantages over other libraries. In this tutorial, we will be specifically focusing on l

4 min read How to Use Android Sliding Activity Library in Android App?

Sliding activities allow you to easily set header content, menus, and data onto slidable screens. Easily create activities that can slide vertically on the screen and fit well into the material design age. We can set header images to our sliding activity. We can also customize the color that will affect the header and the status bar. We can also di

3 min read Android Manifest File in Android

Every project in Android includes a Manifest XML file, which is AndroidManifest.xml, located in the root directory of its project hierarchy. The manifest file is an important part of our app because it defines the structure and metadata of our application, its components, and its requirements. This file includes nodes for each of the Activities, Se

5 min read Android | Android Application File Structure

It is very important to know about the basics of Android Studio's file structure. In this article, some important files/folders, and their significance is explained for the easy understanding of the Android studio work environment. In the below image, several important files are marked: All of the files marked in the above image are explained below

3 min read What's Android Interface Definition Language (AIDL) in Android?

The Android Interface Definition Language (AIDL) is analogous to other IDLs you would possibly have worked with. It allows you to define the programming interface that both the client and repair agree upon so as to speak with one another using interprocess communication (IPC). Traditionally the Android way a process cannot access the memory of some

7 min read Android | Running your first Android app

After successfully Setting up an Android project, all of the default files are created with default code in them. Let us look at this default code and files and try to run the default app created. The panel on the left side of the android studio window has all the files that the app includes. Under the java folder, observe the first folder containi

3 min read How to Clone Android Project from GitHub in Android Studio?

Android Studio provides a platform where one can build apps for Android phones, tablets, Android Wear, Android TV, and Android Auto. Android Studio is the official IDE for Android application development, based on IntelliJ IDEA. One can develop Android Applications using Kotlin or Java as the Backend Language, and it provides XML for developing Fro

3 min read How to Add OpenCV library into Android Application using Android Studio?

OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today’s systems. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a human. When it integrated with various libraries,

3 min read 8 Best Android Libraries That Every Android Developer Should Know

Android is an operating system which is built basically for Mobile phones. It is used for touchscreen mobile devices like smartphones and tablets. But nowadays these are utilized in Android Auto cars, TV, watches, camera, etc. Android has been one of the best-selling OS for smartphones. Android OS was developed by Android Inc. which Google bought i

5 min read How to Add Audio Files to Android App in Android Studio?

The audio file format is a file format for saving digital audio data on a computer system and all are aware of audio files. So in this article, we are going to discuss how can we add audio files to the android app. There are three major groups of audio file formats: Format Name Description Examples Uncompressed audio formatsUncompressed audio forma

3 min read Different Ways to Change Android SDK Path in Android Studio

Android SDK is one of the most useful components which is required to develop Android Applications. Android SDK is also referred to as the Android Software Development Kit which provides so many features which are required in Android which are given below: A sample source code.An Emulator.Debugger.Required set of libraries.Required APIs for Android

5 min read How to Capture a Screenshot and Screen Recording of an Android Device Using Android Studio?

Android Studio is the official IDE (Integrated Development Environment) for Android app development and it is based on JetBrains’ IntelliJ IDEA software. Android Studio provides many excellent features that enhance productivity when building Android apps. Whenever we are developing an app and in that device, you don't know how to capture screenshot

2 min read Different Ways to Fix “Select Android SDK” Error in Android Studio

Android SDK is one of the most useful components which is required to develop Android Applications. Android SDK is also referred to as the Android Software Development Kit which provides so many features which are required in Android which are given below: A sample source code.An Emulator.Debugger.Required set of libraries.Required APIs for Android

5 min read Different Ways to Analyze APK Size of an Android App in Android Studio

APK size is one of the most important when we are uploading our app to Google Play. APK size must be considered to be as small as possible so users can visit our app and download our app without wasting so much data. In this article, we will take a look at How we can analyze our APK in Android Studio to check the APK size. Android Studio itself pro

3 min read Different Ways to fix "Error running android: Gradle project sync failed" in Android Studio

Gradle is one of the most important components of Android apps. It handles all the backend tasks and manages all the external dependencies which we will be going to use in building our Android Application. Sometimes due to any issue or after formatting of your pc. Some of the Gradle files may get deleted unexpectedly. So when you will start buildin

3 min read

How to Fix “Failed to install the following Android SDK packages as some licenses have not been accepted” Error in Android Studio?

When you download the latest Android SDK tools version using the command line to install SDKs and you just try to build gradle then this error shows up: Failed to install the following Android SDK packages as some licenses have not been accepted. platforms;android-27 Android SDK Platform 27 build-tools;27.0.3 Android SDK Build-Tools 27.0.3 To build

4 min read How to fix "Android Studio doesn't see device" in Android Studio?

In Android Studio, sometimes the list of devices and emulators doesn't list your physical device when you try to plug it in. Or you may have faced a situation when you plugged the phone in for the first time, no dialog appeared asking if you trust the computer. But the laptop is already allowed to connect to the mobile device. However, the button R