open a URL in android' web browser

Hi folks, so here I am going to discuss about How to open an URL in Android’s web browser ? , Open a URL in browser , How to use Intent to Open a URL in browser and open URL in browser android example .

Open the Web browser :

To open the web browser from your application, you don’t have to implement a web browser instead of it you can use an existing one like chrome etc. It is a type of implicit Intent. In this example, we are going to open an URL in web browser from your app by using an Intent object that launches existing web browser.

In case if you are not aware of creating a menu in android check this article: Android Menu | Different types of menu in android

Here’s a code snippet to show you how to use intent to open an URL in web browser .

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(“http://technxt.net”));
startActivity(intent);

1. Create a New Project :

Create a new project in Android Studio from File > New Project and select Empty Activity from templates to open an URL in android’s web browser .

2. Add a Button to your layout :

Add a button to your activity_main.xml or xml layout file .

 <Button
        android:id="@+id/button"
        android:layout_width="172dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="215dp"
        android:text="WebView" />

Now open res -> layout -> activity_main.xml or xml layout file and just implement the following xml code .

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TestActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="172dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="215dp"
        android:text="WebView" />
</RelativeLayout>

3. Implement setOnClickListener on your button to make listener on it :

webView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://technxt.net"));
                startActivity(intent);
            }
        });

4. Open your MainActivity.java and add the following code :

package net.technxt;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class TestActivity extends AppCompatActivity {
    Button webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        webView = (Button)findViewById(R.id.button);

        webView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://technxt.net"));
                startActivity(intent);
            }
        });
    }
}

Now run the project/app to test webView button to open a URL in browser in android app through android device or any Emulator .

%d bloggers like this: