Open URL from Android Application in Default Browser

In an Android Application, it may be necessary to open a URL separately in a browser. The task of opening a URL can be triggered by any action performed on Views using Action Listeners.

In this tutorial, we shall look into an application, where we click a button, and a URL is opened in a browser window.

From the following screenshot, when user clicks on the button, a new Browser Activity is displayed over this Android Application, with the URL specified for that page.

Android Open URL in Browser Activity

Open URL from Android Application in Default Browser

To open an URI in a browser window separately, use android.content.Intent.ACTION_VIEW. ACTION_VIEW is used to display data to the user using the most reasonable presentation. For example, when used on a mailto, ACTION_VIEW opens a mail compose window; and when used on tel: , ACTION_VIEW displays call dialer.

Following are the detailed steps to create ACTION_VIEW for opening a URL.

Step 1: Create an Intent for ACTION_VIEW.

</>
Copy
 val openURL = Intent(android.content.Intent.ACTION_VIEW)

Step 2: Set Uri as data for the intent.

</>
Copy
 openURL.data = Uri.parse("https://www.tutorialkart.com/")

Step 3: Start Activity with the intent.

</>
Copy
 startActivity(openURL)

Example – Android Application to Open URL in Default Browser

Create an Android Project and replace the following layout file, activity_main.xml, and MainActivity.kt file.

activity_main.xml

</>
Copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/googleBtn"
        android:text="Google"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/tutorialkartBtn"
        android:text="Tutorial Kart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.kt

</>
Copy
package com.tutorialkart.openurlinbrowseractivity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import android.content.Intent
import android.net.Uri


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        googleBtn.setOnClickListener {
            val openURL = Intent(android.content.Intent.ACTION_VIEW)
            openURL.data = Uri.parse("https://www.google.com/")
            startActivity(openURL)
        }

        tutorialkartBtn.setOnClickListener {
            val openURL = Intent(android.content.Intent.ACTION_VIEW)
            openURL.data = Uri.parse("https://www.tutorialkart.com/")
            startActivity(openURL)
        }
    }
}

Run this Android Application, and click on any of the buttons, to open the respective URL separately in default Browser activity.

Conclusion

In this Kotlin Android Tutorial, we learned how to open URL from Android Application in default browser.