🚀 Build a Simple Web Browser in Android Using WebView in Java – Full Guide with Source Code
Looking to create a Simple Web Browser in Android Using WebView in Java that mimics Google Chrome? In this tutorial, we’ll walk you through building a full-featured, modern web browser in Java using Android Studio. This browser includes essential features like web page loading, navigation buttons, progress bar, and URL input with “GO” button support — all wrapped in a sleek UI similar to Chrome.
🔧 Features of This Android Web Browser
- URL input bar with IME GO button handling
- Full WebView support with JavaScript enabled
- Progress bar to indicate page loading status
- Back and Forward navigation buttons
- Proper back press handling using OnBackPressedDispatcher
📱 MainActivity.java – Complete Code Overview
The heart of our browser lies in the MainActivity.java
. Here’s what each component does:
public class MainActivity extends AppCompatActivity {
private WebView webView;
private EditText urlInput;
private ProgressBar progressBar;
private ImageButton backButton, forwardButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getOnBackPressedDispatcher().addCallback(this, backPressedCallback);
urlInput = findViewById(R.id.urlInput);
webView = findViewById(R.id.webView);
progressBar = findViewById(R.id.progressBar);
backButton = findViewById(R.id.backButton);
forwardButton = findViewById(R.id.forwardButton);
// Enable JavaScript
webView.getSettings().setJavaScriptEnabled(true);
// Handle page load progress
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
progressBar.setVisibility(progress == 100 ? View.GONE : View.VISIBLE);
}
});
// Load initial page
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.google.com");
// Handle "GO" key in soft keyboard
urlInput.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_GO || actionId == EditorInfo.IME_ACTION_DONE) {
loadUrlFromInput();
return true;
}
return false;
});
// Navigation buttons
backButton.setOnClickListener(v -> {
if (webView.canGoBack()) webView.goBack();
});
forwardButton.setOnClickListener(v -> {
if (webView.canGoForward()) webView.goForward();
});
}
private void loadUrlFromInput() {
String url = urlInput.getText().toString().trim();
if (!url.startsWith("http")) {
url = "https://" + url;
}
webView.loadUrl(url);
}
// Handle back press using OnBackPressedDispatcher
private final OnBackPressedCallback backPressedCallback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
}
};
}
🧱 activity_main.xml – Layout Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp">
<ImageButton
android:id="@+id/backButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@android:color/transparent"
android:contentDescription="Back"
android:src="@android:drawable/ic_media_previous" />
<EditText
android:id="@+id/urlInput"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@drawable/et_bg"
android:hint="Enter URL"
android:imeOptions="actionGo"
android:inputType="textUri"
android:lines="1"
android:layout_marginStart="3dp"
android:layout_marginEnd="3dp"
android:paddingStart="8dp"
android:paddingEnd="8dp" />
<ImageButton
android:id="@+id/forwardButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@android:color/transparent"
android:contentDescription="Forward"
android:src="@android:drawable/ic_media_next" />
</LinearLayout>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="4dp"
android:indeterminate="false"
android:max="100" />
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
Output
🎨 UI Design Tips
- Use a clean layout with a URL input bar on top.
- Keep navigation buttons intuitive with back/forward arrows.
- Use a small progress bar under the input to show load progress.
📥
Click here to Download
the source code or ask questions in the comments below!