What is WebView In Android Studio

In this tutorial we will learn how to create WebView in android studio. Creating a WebView is simple. Follow the below steps.

WebView In Android is used to load any website in the application. Usually it takes the URL of the website and loads all the content into the android app. To load the website into the app just put the URL in the WebView.

Webview in android
Webview in android

WebView In Android Programmatically

To create the WebView in android programmatically, take the WebView Widget in your xml file. If you want to load the whole screen then take the size match_parent.

To load WebView just use following code in the project.

webview.loadUrl("https://www.fluttertpoint.com/");

Please note that We used databinding in this project to find the ids from layout. You can quick learn that how to use databinding in android from here.

Complete Code For Simple Loading WebView In Android

package com.example.androidtestingproject.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.databinding.DataBindingUtil;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.FileUtils;
import android.view.View;

import com.example.androidtestingproject.R;
import com.example.androidtestingproject.databinding.ActivityDemoBinding;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class DemoActivity extends AppCompatActivity {
private ActivityDemoBinding binding;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this,R.layout.activity_demo);

initView();
}

private void initView() {
binding.webview.loadUrl("https://www.fluttertpoint.com/");

}

}

WebView Screenshot

WebView in Android
WebView in Android

How To Load WebView Link In WebView Without Redirecting To Browser

If you click on the link shown in the WebView, it will not open in the WebView. To load the link inside the WebView you have to extend the class to the WebViewClient.

class MyTest extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;

}
}

How to Use WebView Method

Apart from loading the URL in the WebView, you can control more things by using the features. Some of the methods are below. You have to override these methods.

canGoBack()

This methods used to go back and specifying the history in WebView.

canGoForward()

We can also go forward if we already back pressed.

clearHistory()

This method specify to clear the backward and forward history.

destroy()

This method is used to destroy the internal state of WebView.

getTitle()

This method return the title of the current webpage.

getUrl()

This method is used to get the URL of current WebView.

getProgress()

This method return the progress of the current page.

These are overrided method in the class. You can use these in your main class to get the entire result as you want.

webview.getUrl();
webview.canGoBack();
webview.canGoForward();
webview.clearHistory();
webview.destroy();
webview.getProgress();

Thank you for visiting this tutorial on flutterTpoint. Please comment for your doubts and issues. We will respond shortly.

close

Don’t miss new tips!

We don’t spam! Read our [link]privacy policy[/link] for more info.

Leave a Comment