tutorial using WebView in Android
tutorial using WebView in Android
Build a simple web app for Android using WebView
Or
The Basic Usage
Integrating a WebView in your app won’t take more than two steps. First you need to include the WebView element in your xml layout.
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
|
Second you have load the specific url in webview from your activity. The below loads google’s homepage into web view.
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("http://google.com");
|
Even though loading a simple url seems very easy, customizing the WebView needs thorough knowledge over WebView and the methods it is providing. We’ll start with basic methods WebView is providing and later on we’ll build a simple browser activity which acts as in-app browser that provides backward, forward and bookmark options. We’ll learn one by one by starting a simple project in Android Studio.
2. Creating New Project
1. Create a new project in Android Studio from File ⇒ New Project by filling the required details.
2. As we need to make network requests, we need to add INTERNET permission in AndroidManifest.xml.
AndroidManifest.xml
|
<?xml version="1.0" encoding="utf-8"?>
package="info.androidhive.webview" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar" >
<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>
|
3. Open build.gradle and add Glide library support. This is required to load the image in CollapsingToolbar. This step is optional, but I suggest you follow it for this article.
build.gradle
|
dependencies {
...
// glide
compile 'com.github.bumptech.glide:glide:3.7.0'
}
|
4. Download this resources folder and add the contents to your project. This folder contains required drawables and assets required for this project.
5. Open the layout files your main activity (activity_main.xml and content_main.xml) and the WebViewelement. Along with this, I am also adding CoordinatorLayout, Toolbar and a ProgressBar which will be shown while the webpage is being loaded.
activity_main.xml
|
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/detail_backdrop_height"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="@android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<ProgressBar
android:id="@+id/progressBar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-7dp"
android:indeterminate="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
|
content_main.xml
content_main.xml
|
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadeScrollbars="false"
android:scrollbarFadeDuration="0"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.NestedScrollView>
|
6. Now open the MainActivity.java and modify the code as below. Here initCollapsingToolbar() method is completely unrelated to WebView, but it is to provide collapsing effect when webpage is scrolled up. The Glide method is used to display the header image in toolbar.
MainActivity.java
|
package info.androidhive.webview;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
public class MainActivity extends AppCompatActivity {
private WebView webView;
private ProgressBar progressBar;
private ImageView imgHeader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
webView = (WebView) findViewById(R.id.webView);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
imgHeader = (ImageView) findViewById(R.id.backdrop);
// initializing toolbar
initCollapsingToolbar();
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(postUrl);
webView.setHorizontalScrollBarEnabled(false);
}
/**
* Initializing collapsing toolbar
* Will show and hide the toolbar txtPostTitle on scroll
*/
private void initCollapsingToolbar() {
final CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle(" ");
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
appBarLayout.setExpanded(true);
// hiding & showing the txtPostTitle when toolbar expanded & collapsed
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow = false;
int scrollRange = -1;
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
}
if (scrollRange + verticalOffset == 0) {
collapsingToolbar.setTitle("Web View");
isShow = true;
} else if (isShow) {
collapsingToolbar.setTitle(" ");
isShow = false;
}
}
});
// loading toolbar header image
Glide.with(getApplicationContext()).load("http://api.androidhive.info/webview/nougat.jpg")
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imgHeader);
}
}
|
If you run the app now, you can see the webpage is loading in WebView. Now we’ll check the other useful methods that web view provides.
2.1 Loading Local Html, CSS and Font-Style
In some cases your might need to load a web page from app’s local storage instead from an url. In order to do that we can keep all the html, css and fonts in assets folder and load them from there.
7. Create a new folder in src/main called assets (src/main/assets) and place your html, css and fonts over there. I am keeping an html file named sample.html. Also I am adding two custom fonts Roboto-Light.ttf and Roboto-Medium.ttf to apply custom font face in CSS. I hope you downloaded these resources from here.
sample.html
|
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Android WebView Tutorial</title>
<style type="text/css">
@font-face {
font-family: 'roboto';
src: url('Roboto-Light.ttf');
}
@font-face {
font-family: 'roboto-medium';
src: url('Roboto-Medium.ttf');
}
body{
color:#666;
font-family: 'roboto';
padding: 0.3em;
}
h1{
color:#5f50e1;
font-family: 'roboto-medium';
}
</style>
</head>
<body>
<h1>WebView loading local html</h1>
This is content is loaded from app's assets folder with custom css and font style <img draggable="false" class="emoji" alt="" src="https://s.w.org/images/core/emoji/2/svg/1f642.svg">
</body>
</html>
|
8. Calling the below code in your MainActivity.java loads the the sample.html from assets folder.
// Loading local html file into web view
webView.loadUrl("file:///android_asset/sample.html");
|
2.2 Enabling / Disabling Javascript
You can enable or disable javascript functionality on a webpage by calling setJavaScriptEnabled() method.
// enable / disable javascript
webView.getSettings().setJavaScriptEnabled(true);
|
2.3 Enabling Zooming Controls
WebView provides in-build zooming controls to zoom-in or zoom-out the webpage. These controls will be very useful when you have difficulty in reading the smaller fonts on webpage. Below code enables zooming controls on the webpage.
/**
* Enabling zoom-in controls
* */
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(true);
|
2.4 Building a Simple In-App Browser
Now with the basic knowledge we have, let’s see how to build a simple browser activity with backward, forward navigations and bookmark option. This in-app browser will be very useful to keep the users in your app instead of navigating them to third browser like Chrome.
9. Create a class named Utils.java and add the below code. Here
> isSameDomain() checks whether an url is from same domain or not. This method will be useful to launch the browser activity incase of external url.
> bookmarkUrl() method adds or removes an url from bookmarks list using SharedPreferences. isBookmarked() checks whether an url is bookmarked or not.
> tintMenuIcon() changes the color of toolbar icon. This method is to change the bookmark icon color when an url is bookmarked.
Utils.java
|
package info.androidhive.webview;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.MenuItem;
/**
* Created by Ravi Tamada on 28/05/16.
* www.androidhive.info
*/
public class Utils {
public static boolean isSameDomain(String url, String url1) {
return getRootDomainUrl(url.toLowerCase()).equals(getRootDomainUrl(url1.toLowerCase()));
}
private static String getRootDomainUrl(String url) {
String[] domainKeys = url.split("/")[2].split("\\.");
int length = domainKeys.length;
int dummy = domainKeys[0].equals("www") ? 1 : 0;
if (length - dummy == 2)
return domainKeys[length - 2] + "." + domainKeys[length - 1];
else {
if (domainKeys[length - 1].length() == 2) {
return domainKeys[length - 3] + "." + domainKeys[length - 2] + "." + domainKeys[length - 1];
} else {
return domainKeys[length - 2] + "." + domainKeys[length - 1];
}
}
}
public static void tintMenuIcon(Context context, MenuItem item, int color) {
Drawable drawable = item.getIcon();
if (drawable != null) {
// If we don't mutate the drawable, then all drawable's with this id will have a color
// filter applied to it.
drawable.mutate();
drawable.setColorFilter(ContextCompat.getColor(context, color), PorterDuff.Mode.SRC_ATOP);
}
}
public static void bookmarkUrl(Context context, String url) {
SharedPreferences pref = context.getSharedPreferences("androidhive", 0); // 0 - for private mode
SharedPreferences.Editor editor = pref.edit();
// if url is already bookmarked, unbookmark it
if (pref.getBoolean(url, false)) {
editor.putBoolean(url, false);
} else {
editor.putBoolean(url, true);
}
editor.commit();
}
public static boolean isBookmarked(Context context, String url) {
SharedPreferences pref = context.getSharedPreferences("androidhive", 0);
return pref.getBoolean(url, false);
}
}
|
10. Under res ⇒ menu, create a new menu called browser.xml. This menu places the back, forward and bookmark icons on toolbar.
browser.xml
|
<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_bookmark"
android:icon="@drawable/ic_bookmark_white_24dp"
android:orderInCategory="100"
android:title="@string/action_bookmark"
app:showAsAction="always" />
<item
android:id="@+id/action_back"
android:icon="@drawable/ic_keyboard_arrow_left_white_24dp"
android:orderInCategory="101"
android:title="@string/action_back"
app:showAsAction="always" />
<item
android:id="@+id/action_forward"
android:icon="@drawable/ic_keyboard_arrow_right_white_24dp"
android:orderInCategory="102"
android:title="@string/action_forward"
app:showAsAction="always" />
</menu>
|
11. Create a new activity called BrowserActivity.java and add the below code. This activity handles the browser forward and backward navigation in the toolbar along with bookmark option.
BrowserActivity.java
|
package info.androidhive.webview;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class BrowserActivity extends AppCompatActivity {
// private String TAG = BrowserActivity.class.getSimpleName();
private String url;
private WebView webView;
private ProgressBar progressBar;
private float m_downX;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browser);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("");
url = getIntent().getStringExtra("url");
if (TextUtils.isEmpty(url)) {
finish();
}
webView = (WebView) findViewById(R.id.webView);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
initWebView();
webView.loadUrl(url);
}
private void initWebView() {
webView.setWebChromeClient(new MyWebChromeClient(this));
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
invalidateOptionsMenu();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
webView.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
invalidateOptionsMenu();
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
progressBar.setVisibility(View.GONE);
invalidateOptionsMenu();
}
});
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
webView.setHorizontalScrollBarEnabled(false);
webView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getPointerCount() > 1) {
//Multi touch detected
return true;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
// save the x
m_downX = event.getX();
}
break;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP: {
// set x so that it doesn't move
event.setLocation(m_downX, event.getY());
}
break;
}
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.browser, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (!webView.canGoBack()) {
menu.getItem(0).setEnabled(false);
menu.getItem(0).getIcon().setAlpha(130);
} else {
menu.getItem(0).setEnabled(true);
menu.getItem(0).getIcon().setAlpha(255);
}
if (!webView.canGoForward()) {
menu.getItem(1).setEnabled(false);
menu.getItem(1).getIcon().setAlpha(130);
} else {
menu.getItem(1).setEnabled(true);
menu.getItem(1).getIcon().setAlpha(255);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if (item.getItemId() == android.R.id.home) {
finish();
}
if (item.getItemId() == R.id.action_back) {
back();
}
if (item.getItemId() == R.id.action_forward) {
forward();
}
return super.onOptionsItemSelected(item);
}
private void back() {
if (webView.canGoBack()) {
webView.goBack();
}
}
private void forward() {
if (webView.canGoForward()) {
webView.goForward();
}
}
private class MyWebChromeClient extends WebChromeClient {
Context context;
public MyWebChromeClient(Context context) {
super();
this.context = context;
}
}
}
|
12. Open the MainActivity.java modify the code as below. Here we use a custom MyWebChromeClient() to intercept the methods of WebView. In shouldOverrideUrlLoading() we detect whether the clicked link is internal or external. If it’s external, we launch the BrowserActivity.
MainActivity.java
|
package info.androidhive.webview;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
public class MainActivity extends AppCompatActivity {
private WebView webView;
private ProgressBar progressBar;
private float m_downX;
private ImageView imgHeader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
webView = (WebView) findViewById(R.id.webView);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
imgHeader = (ImageView) findViewById(R.id.backdrop);
if (!TextUtils.isEmpty(getIntent().getStringExtra("postUrl"))) {
postUrl = getIntent().getStringExtra("postUrl");
}
initWebView();
initCollapsingToolbar();
renderPost();
}
private void initWebView() {
webView.setWebChromeClient(new MyWebChromeClient(this));
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
/**
* Check for the url, if the url is from same domain
* open the url in the same activity as new intent
* else pass the url to browser activity
* */
if (Utils.isSameDomain(postUrl, url)) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
intent.putExtra("postUrl", url);
startActivity(intent);
} else {
// launch in-app browser i.e BrowserActivity
openInAppBrowser(url);
}
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
});
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
webView.setHorizontalScrollBarEnabled(false);
webView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getPointerCount() > 1) {
//Multi touch detected
return true;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
// save the x
m_downX = event.getX();
}
break;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP: {
// set x so that it doesn't move
event.setLocation(m_downX, event.getY());
}
break;
}
return false;
}
});
}
private void renderPost() {
webView.loadUrl(postUrl);
}
private void openInAppBrowser(String url) {
Intent intent = new Intent(MainActivity.this, BrowserActivity.class);
intent.putExtra("url", url);
startActivity(intent);
}
/**
* Initializing collapsing toolbar
* Will show and hide the toolbar txtPostTitle on scroll
*/
private void initCollapsingToolbar() {
final CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle(" ");
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
appBarLayout.setExpanded(true);
// hiding & showing the txtPostTitle when toolbar expanded & collapsed
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow = false;
int scrollRange = -1;
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
}
if (scrollRange + verticalOffset == 0) {
collapsingToolbar.setTitle("Web View");
isShow = true;
} else if (isShow) {
collapsingToolbar.setTitle(" ");
isShow = false;
}
}
});
// loading toolbar header image
Glide.with(getApplicationContext()).load("http://api.androidhive.info/webview/nougat.jpg")
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imgHeader);
}
private class MyWebChromeClient extends WebChromeClient {
Context context;
public MyWebChromeClient(Context context) {
super();
this.context = context;
}
}
}
|
Run the app and click on external link in the webpage. You can see the in-app browser launched with the navigation.
Here the code block that launches the in-app browser. Pass the intended url that you want to load.
Intent intent = new Intent(MainActivity.this, BrowserActivity.class);
intent.putExtra("url", url);
startActivity(intent);
|
I hope this article clears most of the doubts you have regarding WebView. Fell free to ask the queries if you have in the comments section below.
ليست هناك تعليقات