Android start url in browser

Kotlin Android – Open URL separately in Browser Activity

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.

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.

Step 2: Set Uri as data for the intent.

Step 3: Start Activity with the intent.

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

MainActivity.kt

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

Читайте также:  Webrtc отключить для андроид

Conclusion

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

Источник

Оцените статью