Simple Android WebView code for accessing a website within app

By Faruque Ahamed Mollick

Here in this post I am going give you simple android WebView code to open a website through an android app. The WebView class is an built in extension of Android’s View class that allows you to display web pages as a part of your activity layout.

If a android developer wants to add browser functionality for browsing a web page or website to an application, the developer can include the WebView library and create an instance of a WebView class. This is not a fully developed web browser, all that WebView do is displaying web page in the layout. Thus you can render web pages and even also JavaScript of the web page easily.

How to add WebView in android the layout?

To add a WebView to your Application, you need to include the <WebView> element in your activity layout XML. Below is an example code:

<WebView
      android:id="@+id/my_webview"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

Then use loadUrl() to load a web page in your application:

WebView myWebView = (WebView) findViewById(R.id.my_webview);
myWebView.loadUrl("https://www.codespeedy.com");

To load the web page in your web view your application must should request the INTERNET permission from the manifest file.

That’s all you need to do for a basic WebView application which loads a web page in the app.

To load the JavaScript from the web page and let it work you need to add this code:

WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

Now you will see that all the link from the app is opening via the default browser of your android device. However it needs to open in the app through WebView. To do this you need to add this code:

mWebView.setWebViewClient(new WebViewClient());

 


Android WebView code with basic features:

I have already created a complete WebView app with these features given below:

  1. Showing a progress bar while loading the web page first time after starting the app.
  2. Check internet connection if it is available and display no internet connection when there are no internet connection is available.

You can buy the complete source code from here only for $3 – https://gumroad.com/l/Android-Studio-WebView-Source-Code.

If you are from India then you can order it from here for Rs 199/- – https://www.instamojo.com/@frkmollick/l0bb442a7ca4a476b89c78f111671d454/.

the source code is mainly for the newbies android developers. You can improve the source code and implement more features.

Leave a Reply

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