What is WebView in Android?

Published by Shivam Pandey on

WebView

In this blog, we will discuss WebView with an example, and I am much sure after hovering this blog keenly you will get to know how to convert a website into an android app. Let’s start,

In Android, WebView is a view used to display the web pages in the 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.

Internet Permission Required In a WebView:

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).

<!--Add this before application tag in AndroidManifest.xml-->
<uses-permission android:name="android.permission.INTERNET" />

XML File

ln order to add WebView in your application, you have to add <WebView> element to your XML( layout ) file

XML file of BrighterBees java class

Method of Webview

  • loadurl(): 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.
  • loaddata(): This method is used to load the static HTML string in a web view. loadData() function takes HTML string data.
  •  Using WebViewClient: WebViewClient helps us to monitor the event in a WebView. You have to Override the shouldOverrideUrlLoading() method. This method allows 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.
  •  canGoBack(): 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.

Java File

Below is the java code how to set website URL and convert website into an android application,

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.shivam.practiceapp.R;


public class BrighterBees extends AppCompatActivity {
    private WebView mywebView;
    private ProgressBar progressbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brighter_bees);
        mywebView=(WebView) findViewById(R.id.webview);
        progressbar=(ProgressBar) findViewById(R.id.progressbar);


        mywebView.setWebViewClient(new WebViewClient());
        mywebView.loadUrl("https://www.brighterbees.com/");
        WebSettings webSettings=mywebView.getSettings();
        mywebView.getSettings().setLoadWithOverviewMode(true);
        mywebView.getSettings().setSupportZoom(true);
        mywebView.getSettings().setJavaScriptEnabled(true);
        mywebView.setWebChromeClient(new WebChromeClient());
    }
    public class WebChromeClient extends android.webkit.WebChromeClient{

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            progressbar.setVisibility(View.VISIBLE);

            progressbar.setProgress(newProgress);
        }
    }
    public class mywebClient extends WebViewClient{
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon){
            super.onPageStarted(view,url,favicon);
        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view,String url){
            view.loadUrl(url);
            return true;
        }
    }
    @Override
    public void onBackPressed(){
        if(mywebView.canGoBack()) {
            mywebView.goBack();
            progressbar.setVisibility(View.GONE);
        }
        else{
            super.onBackPressed();
        }
    }

THANK YOU FOR READING THIS POST.

CONNECT WITH THE AUTHOR ON LINKEDIN.

IF YOU WANT TO KNOW ABOUT ANDROID DEVELOPMENT click here.

VISIT OUR WEBSITE BRIGHTERBEES FOR MORE INFORMATION OF ANDROID DEVELOPMENT.


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

STAY CONNECT WITH US