Android WebView example tutorial

Creating android apps using webView to display website through given String URL.

Webview is a type of frame layout used to display websites using given string url format. That means app developer can pass any url( Universe resource locator ) through string variable and it will open that webpage on activity screen. So here is the complete step by step tutorial for Android WebView example tutorial.

android-project-download-code-button

Android WebView example tutorial.

Note: Webview used internet connection to display webpage so add internet permission inside AndroidManifest.xml file.
 <uses-permission android:name=”android.permission.INTERNET” />

Code for MainActivity.java file.

 package com.android_examples.com.webviewexample;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;


public class MainActivity extends Activity {

 WebView webview;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 webview = (WebView)findViewById(R.id.webView1);
 WebSettings webSetting = webview.getSettings();
 webview.setWebViewClient(new WebViewClient());
 
 //define your website url here which you want to open in webview.
 webview.loadUrl("https://www.android-examples.com");
 }

 private class WebViewClient extends android.webkit.WebViewClient
 {
 @Override
 public boolean shouldOverrideUrlLoading(WebView view, String url) 
 {
 return super.shouldOverrideUrlLoading(view, url);
 }
 }
 
}

Code for activity_main.xml layout file.

 <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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.webviewexample.MainActivity"
 >

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

</RelativeLayout>

 

Code for AndroidManifest.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.android_examples.com.webviewexample"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
 android:minSdkVersion="8"
 android:targetSdkVersion="21" />

 <uses-permission android:name="android.permission.INTERNET" />
 
 <application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
 android:name=".MainActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

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

</manifest>

Screenshots:

Android WebView example tutorial

Android WebView example tutorial

Click Here To Download Android WebView example tutorial project.