Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaHow to Open a URL in Android’s Web Browser in an Android...

How to Open a URL in Android’s Web Browser in an Android Application?

It might be essential to launch a browser separately while using an Android application to access a URL. Any action made on Views using Action Listeners has the potential to initiate the task of accessing a URL. In this article, we will learn about How we can Open a URL in Android’s Web Browser from an Android Application.

For this article, we will be using the concept of Intent.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Project just refer to this article on How to Create New Project in Android Studio. The code has been given in both Java and Kotlin Programming Language for Android.

Step 2: Working with the activity_main.xml File

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:id="@+id/layout">
        <TextView
            android:textSize="20dp"
            android:layout_marginTop="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="https://www."
            android:id="@+id/textview" />
 
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/urltxt"
            android:textColor="@color/black"
            android:layout_marginEnd="20dp"
            android:layout_centerInParent="true"
            android:alpha="0.7"
            android:textSize="20dp"
            android:padding="10dp" />
    </LinearLayout>
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="submit"
        android:layout_below="@+id/layout"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:id="@+id/gotoURL" />
</RelativeLayout>


Step 3: Working with MainActivity File

Navigate to app > java > your app’s package name > MainActivity File (Java or Kotlin) and add the below code to it. Comments are added in the code to get to know in detail.

Kotlin




import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
 
class MainActivity : AppCompatActivity() {
     
    private lateinit var editText: EditText
    private lateinit var button: Button
    private lateinit var textView: TextView
     
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        title = "neveropen"
        setContentView(R.layout.activity_main)
         
        editText = findViewById(R.id.urltxt)
        button = findViewById(R.id.gotoURL)
        textView = findViewById(R.id.txtview)
         
        button.setOnClickListener {
            val url = editText.text.toString()
            val urlIntent = Intent(
                Intent.ACTION_VIEW,
                Uri.parse(textView.text.toString() + url)
            )
            startActivity(urlIntent)
        }
    }
}


Java




import android.content.Intent;
import android.net.Uri;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
 
    EditText editText;
    Button button;
    TextView textView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("neveropen");
        setContentView(R.layout.activity_main);
 
        editText = findViewById(R.id.urltxt);
        button = findViewById(R.id.gotoURL);
        textView = findViewById(R.id.txtview);
 
        button.setOnClickListener(view -> {
            String url = editText.getText().toString();
            Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(textView.getText().toString() + url));
            startActivity(urlIntent);
        });
    }
}


We will predefine the URL part “https://www” with underlined so that it can look like actual EditText. 

Output:

Open a URL in Android's Web Browser in an Android Application

 

RELATED ARTICLES

Most Popular

Recent Comments