Opening a URL in Android’s Web Browser from Your Application

Opening a URL in Android's Web Browser from Your Application

22 May 2024 Stephan Petzl Leave a comment Tech-Help

As an Android developer, you might encounter scenarios where you need to open a URL in the device’s default web browser rather than within your application. This guide provides a clear and practical approach to achieve this functionality.

Using Intents to Open URLs

The most common and effective way to open a URL in the default web browser is by using Intents. Below is a step-by-step method to do this:

Basic Implementation

You can use the following code snippet to open a URL:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(""));

This method creates an Intent with the action Intent.ACTION_VIEW and the URL as a URI. It then starts an activity to handle this Intent.

Handling Missing Protocols

It’s essential to ensure that the URL starts with “http://” or “https://”. You can add a check to handle this:

if (!url.startsWith("http://") && !url.startsWith("https://")) {
    url = "http://"+ url;
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

This snippet checks if the URL starts with the correct protocol and adds “http://” if it doesn’t.

Using Kotlin

If you are developing in Kotlin, the approach remains similar. Here is the Kotlin equivalent:

if (!url.startsWith("http://") && !url.startsWith("https://")) {
    url = "http://$url";
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url));

Checking for Available Applications

To avoid crashes when no application can handle the Intent, you should verify that an app exists to receive the Intent:

public void openWebPage(String url) {
    Uri webpage = Uri.parse(url);
    Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
    if (intent.resolveActivity(getPackageManager()) != null) {

This method uses resolveActivity() to check if there is at least one app that can handle the Intent before calling startActivity().

Advanced Techniques

For more advanced use cases, such as opening URLs in Chrome Custom Tabs, you can refer to specific documentation or blog articles. Here are a few resources to get you started:

Enhancing Your Development Workflow

While handling URLs effectively is crucial, ensuring your application is well-tested is equally important. This is where Repeato comes into play. Repeato is a no-code test automation tool for iOS and Android, designed to help you create, run, and maintain automated tests for your apps quickly and efficiently.

With Repeato, you can:

  • Focus on creating a great product instead of spending time on maintaining tests.
  • Leverage computer vision and AI for fast and reliable test automation.
  • Delegate test automation tasks to non-technical colleagues or QAs.

Learn more about Repeato and how it can streamline your development process on our About page.

Like this article? there’s more where that came from!