Convert a Website into an Android App with WebView
Turn any website into an Android app with WebView. Links stay inside your app, JavaScript works, and the back button behaves the way users expect.
What you'll learn
- Show a website with WebView
- Keep links inside your app with WebViewClient
- Turn on JavaScript
- Make the back button go back in the page history
Before you start
- Android Studio (any recent version)
- Basic Java: variables, methods and if/else
- An emulator or an Android phone
How it fits together
- 1App opens
- 2WebView loads HOME_URL
- 3Your links stay in the app
- 4Other links open the browser
Build it step by step
1. Create the project
In Android Studio choose New Project → Empty Views Activity, with Language Java and Minimum SDK API 24.
2. Allow internet access
In AndroidManifest.xml, add <uses-permission android:name="android.permission.INTERNET" /> above the application element. Without it, every page fails to load.
3. Add the WebView
In activity_main.xml, add a thin horizontal ProgressBar and a WebView that fills the rest of the screen.
4. Load your website
In MainActivity, find the WebView and call loadUrl(HOME_URL). Change HOME_URL to your own site.
5. Turn on JavaScript
Most sites need it: getSettings().setJavaScriptEnabled(true). Also turn on DOM storage, which many modern sites use. Only do this for a site you trust.
6. Keep your links inside the app
Without a WebViewClient, tapping a link opens the phone's browser. In shouldOverrideUrlLoading, return false for links on your own site, so they load in the app. Open everything else (other sites, email and phone links) with an Intent and return true.
7. Show loading progress
A WebChromeClient reports progress from 0 to 100. Set it on the progress bar, show the bar when a page starts, and hide it when it finishes.
8. Make Back go back
Add an OnBackPressedCallback: if webView.canGoBack(), go back a page; otherwise let the app close.
9. Run it
Tap around your site, then press Back to step back through the pages. Turn on airplane mode and reload to see the offline message.
If something goes wrong
- Blank screen or an error page: check the INTERNET permission and that the address starts with
https://. - Links open Chrome: set your
WebViewClientbefore callingloadUrl. - Your site looks broken: turn on JavaScript and DOM storage.
What's improved over the video
A loading bar, Back that walks through the pages with the current API, outside links opened safely, an offline message, and the page kept when you rotate the phone.
Files affected
Check what to add and what to replace before copying code.
Application code
Select a file to see its code and where it belongs.
This code uses package com.example.webviewapp. Change that first line to your own package, the one at the top of your MainActivity.
app/src/main/java/com/example/webviewapp/MainActivity.javaLoads your site, shows loading progress, keeps your links in the app and handles Back.
package com.example.webviewapp;
import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.activity.OnBackPressedCallback;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
/** Shows a website inside the app, like a mini browser locked to one site. */
public class MainActivity extends AppCompatActivity {
// Change this to your own website.
private static final String HOME_URL = "https://codelab-x.com/";
WebView webView;
ProgressBar progress;
@SuppressLint("SetJavaScriptEnabled") // the site is ours, so JavaScript is safe to allow
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets bars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(bars.left, bars.top, bars.right, bars.bottom);
return insets;
});
webView = findViewById(R.id.webview);
progress = findViewById(R.id.progress);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true); // many modern sites need local storage
webView.setWebViewClient(new MyWebClient());
// WebChromeClient reports loading progress for the bar at the top.
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
progress.setProgress(newProgress);
}
});
// After a rotation, restore the page the user was on instead of starting over.
if (savedInstanceState == null || webView.restoreState(savedInstanceState) == null) {
webView.loadUrl(HOME_URL);
}
// Back goes back through the web pages first, and only leaves the app at the first page.
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
setEnabled(false);
getOnBackPressedDispatcher().onBackPressed();
}
}
});
}
private class MyWebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progress.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
progress.setVisibility(View.GONE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Uri url = request.getUrl();
// Pages on our own site stay inside the app.
if (url.getHost() != null && url.getHost().endsWith(Uri.parse(HOME_URL).getHost())) {
return false;
}
// Everything else (other sites, mail, phone links) opens in the right app.
try {
startActivity(new Intent(Intent.ACTION_VIEW, url));
} catch (ActivityNotFoundException e) {
Toast.makeText(MainActivity.this, R.string.no_app, Toast.LENGTH_SHORT).show();
}
return true;
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
if (request.isForMainFrame()) {
Toast.makeText(MainActivity.this, R.string.offline, Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
webView.saveState(outState);
}
}


