Create Android Webview Application with progressbar

Hello Reader Today i will show you a tutorial on how to build android webview Android application for your website. If you dont have android studio installed please read instructions how to install  here How to Download and Set Up Android Studio.

After Everything has been set up Create a new project, give it a name preferable name of your site and continue.



Select Minimum SDK as you can see mine below i choosed Android API 15 4.0.3 and Next to continue.


Choose Empty activity and continue, Your new app project will be created and ready for you to start coding and implementing the webview.

Navigate to res/layout and open activity_main


Switch to from Design to Text mode



Clear everything and type this

<?xml version="1.0" encoding="utf-8"?>
<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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
tools:context="com.smartbingari.webviewapp.MainActivity">

<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />


</RelativeLayout>

This Above is the Layout file, The Webview tells the layout that a webview will be displayed in it and with the giveen attributes like the height, width and alignments.

And ProgressBar displays a rotating bar to show when page is loading progress.


Now go to MainActivity in app/com.smartbingari.webviewapp
and type the following code:

package com.smartbingari.webviewapp;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends AppCompatActivity {

private WebView mwebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//WebView
mwebView = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = mwebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//improve webView performance
mwebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
mwebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
mwebView.getSettings().setAppCacheEnabled(true);
mwebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);

mwebView.loadUrl("http://www.smartbingari.com");
//force links open in webview only
mwebView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {

super.onPageStarted(view, url, favicon);
findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
}

@Override
public void onPageFinished(WebView view, String url) {
findViewById(R.id.progressBar).setVisibility(View.GONE);
}
});
}

//goto previous page when pressing back button

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mwebView.canGoBack()) {
mwebView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}

}



And finally Your android App requires Internet permission to use internet to load your url.
In your AndroidManifest.xml file paste this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smartbingari.webviewapp">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>





The Line <uses-permission android:name="android.permission.INTERNET" />
Allows your app to connect to the Internet.

You can now run your application.


Finally your result should look something like this.


Congratulations on your new webview application. Follow this blog to get more tutorials.
incase you encounter any error or have any question please use the comment box below
Create Android Webview Application with progressbar Create Android Webview Application with progressbar Reviewed by Mr. SAM on December 31, 2017 Rating: 5

No comments:

Powered by Blogger.