Visual Studio Code Webview Example

WebView Tutorial With Example In Android Studio

Using WebView for WinForms Using the Designer. First, we’ll need to add a WinForms control from a NuGet package to the Toolbox in Visual Studio. In a future release, Visual Studio will do this automatically. First, open the Visual Studio Toolbox, then right-click anywhere in the toolbox, and select the Choose Items. The WebView control shows web content in your Windows Forms or WPF desktop application. This is one of several wrapped Universal Windows Platform controls that are available for Windows Forms and WPF applications. For more information, see UWP controls in desktop applications. This control uses the Microsoft Edge rendering engine (EdgeHTML) to embed a view that renders richly formatted HTML5.

In Android, WebView is a view used to display the web pages in application. This class is the basis upon which you can roll your own web browser or simply use it to display some online content within your Activity. We can also specify HTML string and can show it inside our application using a WebView. Basically, WebView turns application into a web application.

In order to add Web View in your application, you have to add <WebView> element to your XML( layout ) file or you can also add it in java class.

Table Of Contents

Internet Permission Required For Webview:

Important Note: In order for Activity to access the Internet and load the web pages in a WebView, we must add the internet permissions to our Android Manifest file (Manifest.xml).

Webview

Below code define the internet permission in our manifest file to access the internet in our application.

Methods of WebView In Android:

Let’s discuss some common methods of a Webview which are used to configure a web view in our application.

loadUrl() – Load a web page in our WebView

This function is used to load a web page in a web view of our application. In this method we specify the url of the web page that should be loaded in a web view.

Below we load a url: https://abhiandroid.com/ui/ in our application.

2. loadData() – Load Static Html Data on WebView

This method is used to load the static HTML string in a web view. loadData() function takes html string data, mime-type and encoding param as three parameters.

Below we load the static Html string data in our application of a web view.

3. Load Remote URL on WebView using WebViewClient:

WebViewClient help us to monitor event in a WebView. You have to Override the shouldOverrideUrlLoading() method. This method allow us to perform our own action when a particular url is selected. Once you are ready with the WebViewClient, you can set the WebViewClient in your WebView using the setWebViewClient() method.

Below we load a url by using web view client in a WebView.

4. canGoBack() – Move to one page back if a back history exist

This method is used to specify whether the web view has a back history item or not. This method returns a Boolean value either true or false. If it returns true then goBack() method is used to move one page back.

Below we check whether a web view has back history or not.

5. canGoForward() – Move one page forward if forward history exist

This method is used to specify whether the web view has a forword history item or not. This method returns a Boolean value either true or false. If it returns true then goForword() method is used to move one page forword.

Below we check whether a web view has forward history or not.

6. clearHistory() – clear the WebView history

This method is used to clear the web view forward and backward history.

Below we clear the forword and backword history of a WebView.

WebView Example In Android Studio:

Here in this WebView example we show the use of web view in our application. To do that we display two buttons one is for displaying a web page and other is for displaying static HTML data in a web view. Below is the final output, download code and step by step explanation:

Visual Studio Code Webview ExampleStep 1:Create a new project and name it WebViewExample

Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code :

In this step we open an XML file and add the code for displaying two buttons and a Webview in our xml file ( layout ).

Step 3: Open src -> package -> MainActivity.java

In this step we open MainActivity and add the code to initiate the web view and two buttons. Out of those one button is used for displaying a web page in a webview and other one is used to load a static HTML page in webview.

Step 4: Open manifests -> AndroidManifest.xml

In this step we open Manifest file and define the internet permission for our app.

C++ Visual Studio Code Example

Output:

Now start the AVD in Emulator and run the App. Choose either to open webpage or static HTML in WebView by clicking on Button. We open static HTML.

Visual Studio Sample Codes

How to add progressBar in webview & convert website into advance android app

Convert Website Into App Using WebView:

Now you have learn WebView, so why not try converting any website into Android App? Read our advance WebView Android App tutorial to learn how to do it easily.


Continue Reading: