If you want to deliver a web application (or just a web page) as a part of a client application, you can do it using WebView
. The WebView
class is an extension of Android’s View
class that allows you to display web pages as a part of your activity layout. It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView
does, by default, is show a web page.
When you want to provide information in your app that you might need to update, such as an end-user agreement or a user guide, using WebView is a common scenario. You can create an Activity in your Android app that contains a WebView, and then use that to display your online document.
Add a WebView to your app
To add a WebView
to your app, you can either include the <WebView>
element in your activity layout, or set the entire Activity window as a WebView
in onCreate()
.
Add a WebView in the activity layout
To add a WebView
to your app in the layout, add the following code to your activity’s layout XML file:
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
To load a web page in the WebView
, use loadUrl()
. For example:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.codingtimes.in");
Add a WebView in onCreate()
To add a WebView
to your app in an activity’s onCreate()
method instead, use logic similar to the following:
WebView myWebView = new WebView(activityContext);
setContentView(myWebView);
Then load the page with:
myWebView.loadUrl("https://www.codingtimes.in");
Or load the URL from an HTML string:
String customHtml = "<html><body><h1>Welcome to Codingtimes</h1>"
+"<h2>Welcome to Codingtimes</h2><h3>Welcome to Codingtimes</h3>"+
"<p>It's a Static Web HTML Content.</p>"+
"</body></html>";
webView.loadData(customHtml,"text/html","UTF-8");
Before this works, however, your app must have access to the Internet. To get internet access, request the INTERNET
permission in your manifest file. For example:
<manifest ... >
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>
That’s all you need for a basic WebView
that displays a web page. Additionally, you can customize your WebView
by modifying the following:
- Enabling fullscreen support with
WebChromeClient
. This class is also called when aWebView
needs permission to alter the host app’s UI, such as creating or closing windows and sending JavaScript dialogs to the user. To learn more about debugging in this context, read Debugging Web Apps. - Handling events that impact content rendering, such as errors on form submissions or navigation with
WebViewClient
. You can also use this subclass to intercept URL loading. - Enabling JavaScript by modifying
WebSettings
. - Using JavaScript to access Android framework objects that you have injected into a
WebView
.
Android WebView Example
Following is the example of showing a static HTML content in WebView in android applications.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView 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:id="@+id/webview">
</WebView>
MainActivity.java
package in.codingtimes.webview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webview);
String customHtml = "<html><body><h1>Welcome to Codingtimes</h1>"
+"<h2>Welcome to Codingtimes</h2><h3>Welcome to Codingtimes</h3>"+
"<p>It's a Static Web HTML Content.</p>"+
"</body></html>";
webView.loadData(customHtml,"text/html","UTF-8");
}
}

Android Show Web URL Content in WebView Example
Now we will see how to load remote URL content in WebView with example in the android application.
By using WebView LoadURL property we can load remote URL content in our android applications. To show the remote URL content in webview modify MainActivity.java file code as shown below.
ManiActivity.java
package in.codingtimes.webview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings= webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://www.codingtimes.in");
// Line of Code for opening links in app
webView.setWebViewClient(new WebViewClient());
}
//Code For Back Button
@Override
public void onBackPressed() {
if(webView.canGoBack())
{
webView.goBack();
}
else
{
super.onBackPressed();
}
}
}
If you observe above example, we are trying to load the remote URL (https://www.codingtimes.in) content in our android application using WebView and we set a property setJavaScriptEnabled() to enable JavaScript because by default the JavaScript is disabled in WebView.


That’s it!
You have successfully completed the post. Do Share : )
Peace Out!
Also Read – Android Intent
Check Out Deals on -> Amazon , Flipkart , Myntra , Adidas , Apple TV , Boat , Canva , Beardo , Coursera , Cleartrip , Fiverr , MamaEarth , Swiggy, KFC
[…] Also Read – Android WebView with Example […]