The Wayback Machine - https://web.archive.org/web/20240913225221/https://www.geeksforgeeks.org/how-to-open-a-url-in-androids-web-browser-in-an-android-application/
Open In App

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

Last Updated : 12 Jan, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 = "geeksforgeeks"
        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("geeksforgeeks");
        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

 



Similar Reads

How to Open an External URL on Button Click in Android using Jetpack compose?
Many times in android applications we have to open a web page from an URL inside the default browser of the user's device. In that case, we have to simply pass that URL to the web browser to load that page. In this article, we will take a look at How to open a URL in an android web browser from our android application using Jetpack Compose. A sampl
5 min read
How to Open PDF From URL in Android Without Any Third Party Libraries?
There are lots of libraries that are available online but it takes lots of space and time to render you can see these articles if you want to load pdf using third-party libraries Load PDF from URL in Android. But today we are going to load PDFs without using any third-party libraries the main purpose of that is it reduce the APK size and it is so e
4 min read
Open Specific Settings Using Android Application
In android development, the phase comes where the app needs specific settings to be modified manually by the user. So at that time developer directs the user to open specific settings and modify them. So in this article, it has been discussed how to open specific settings and make the user change them easily. Step 1: Create a new Empty Activity and
3 min read
How to Check if Application is Installed in Your Android Phone and Open the App?
In this article, we are going to check if a particular app is installed on our phones or not. If yes then we will be having an option to open the app. Else it will show a toast message saying Not available. So here we are going to learn how to implement that feature. Note that we are going to implement this project using the Java language. Step by
3 min read
How to Open Chrome Browser Using Selenium in Java?
Selenium is an open-source popular web-based automation tool. The major advantage of using selenium is, it supports all browsers like Google Chrome, Microsoft Edge, Mozilla Firefox, and Safari, works on all major OS, and its scripts are written in various languages i.e Java, Python, JavaScript, C#, etc. We will be working with Java. In this article
3 min read
How to Open Microsoft Edge Browser using Selenium in Java?
Selenium is an open-source popular web-based automation tool. The major advantage of using selenium is, it supports all browsers like Google Chrome, Microsoft Edge, Mozilla Firefox, and Safari, works on all major OS and its scripts are written in various languages i.e Java, Python, JavaScript, C#, etc. We will be working with Java. In this article,
3 min read
How to Create a Selenium Maven Project with Eclipse to Open Chrome Browser?
Maven is a Yiddish word meaning accumulator of knowledge, Maven is a build automation tool. It is a software project management and comprehension tool that can manage the project’s build, reporting, and documentation. Selenium is an open-source popular web-based automation tool. The major advantage of using selenium is that it supports all browsers
3 min read
How to Open a Browser in Headless Mode in Selenium using Java?
Selenium is an open-source web automation tool that supports many user actions to perform testing in the web browser. Why Headless Browser? Executing the automation code to run on a web browser is used to perform UI testing and functional testing, But sometimes while we run our automation scripts on the browser the page takes time to load all the U
2 min read
Web Container and Web Application Project Setup in Java
Web container is a web server component that interacts with the Java Servlets. This processes the requests to the requested servlet while checking the required access rights of the URL. The functioning is as follows: The web client requests a URL to the web server.The web container in the server maps it to the requested servlet.The servlet then loa
2 min read
Android | Android Application File Structure
It is very important to know about the basics of Android Studio's file structure. In this article, some important files/folders, and their significance is explained for the easy understanding of the Android studio work environment. In the below image, several important files are marked: All of the files marked in the above image are explained below
3 min read
How to Add OpenCV library into Android Application using Android Studio?
OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today’s systems. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a human. When it integrated with various libraries,
3 min read
Android | How to add Radio Buttons in an Android Application?
Android radio button is a widget that can have more than one option to choose from. The user can choose only one option at a time. Each option here refers to a radio button and all the options for the topic are together referred to as Radio Group. Hence, Radio Buttons are used inside a RadioGroup. Pre-requisites: Android App Development Fundamental
6 min read
Spring Boot - application.yml/application.yaml File
Spring is widely used for creating scalable applications. For web applications Spring provides. In Spring Boot, whenever we create a new Spring Boot Application in spring starter, or inside an IDE (Eclipse or STS) a file is located inside the src/main/resources folder named as application.properties file which is shown in the below media: So in a s
4 min read
How to Load Any Image From URL Without Using Any Dependency in Android?
Many applications display images from the internet using third-party APIs like Glide and Picasso to load images. This means that such applications partly depend on these services to keep themselves working fine. To make the application better, one should write their own code rather than depending on such services. In this article, we will show you
3 min read
How to Load SVG from URL in Android ImageView?
It is seen that many Android apps require to use of high-quality images that will not get blur while zooming. So we have to use high-quality images. But if we are using PNG images then they will get blur after zooming because PNG images are made up of pixels and they will reduce their quality after zooming. So SVG images are preferable to use becau
4 min read
How to Load PDF from URL in Android?
Most of the apps require to include support to display PDF files in their app. So if we have to use multiple PDF files in our app it is practically not possible to add each PDF file inside our app because this approach may lead to an increase in the size of the app and no user would like to download the app with such a huge size. So to tackle this
4 min read
How to Play Audio from URL in Android?
Many apps require the feature to add the audio feature in their application and there so many audio files that we have to play inside our application. If we will store so many audio files inside our application, then it will increase the size of the app and this may reduce the user base due to the huge app size. So the better way to tackle this pro
4 min read
How to Generate API URL for Public Instagram Feeds in Android?
If we are want to display any data from Instagram inside our application, then we will have to add an API URL for Instagram to load data from Instagram. So we have to generate this URL to get data from our Instagram account. In this article, we will take a look at creating an Instagram API using No Code API. Step by Step Implementation for the gene
5 min read
How to Set Restriction For URL in Android EditText?
In this article, we are going to set restrictions in our EditText for URL. Whenever we are creating any form and we want to get the portfolio website of the user or even if we want to restrict the user to write the only URL then we can use this feature. This enables a user to user to write URL only. Step By Step Implementation Step 1: Create a New
2 min read
How to Download File from URL in Android Programmatically using Download Manager?
In this article, we are going to learn how to download files from an URL using Download Manager. Here we will be simply adding the link of the file available online. When we click on the Button it will be downloaded automatically to our phone storage. This is a very frequently used feature as we can use this feature to automatically download the fa
2 min read
How to Display Progress in ProgressBar While Loading URL in WebView in Android?
ProgressBar in Android is a UI element that displays the status of some work being done in the background in the form of some animation. It can be anything, like computing, loading, searching, fetching, downloading, etc. ProgressBar broadly has two types namely spinning wheel and horizontal bar. In this article, we want to show you how you could im
2 min read
Download Image From URL in Android
In Android, ImageView is used to display images locally and externally. Locally, images can be fetched from devices memory or from project folders is available. Externally, images can be loaded from websites. The URL of the image present on the website is decoded and can be displayed using a bitmap factory. Bitmap factory helps convert image to a B
4 min read
Load PDF From URL in Android with Kotlin
PDF View is most of the applications to display the PDF files within the application to display the pdf within our own application rather than redirecting to another application. If we want to display multiple pdf files within our application we have to host these files and then access these files with the help of their URL. In this article, we wil
4 min read
How to Load Image From URL in Android using Jetpack Compose?
Most of the applications in android use images for displaying some useful information within the applications. Images are considered as easy to convey information to the user. An android application uses more than 100 different types of images. So it is not practically possible to add all the images within the application. This will increase the ap
4 min read
How to Load SVG From URL in Android ImageView with Kotlin?
Many websites use images of different types within their project. They may be SVG, png as well as jpeg image format. It is easy to load png and jpeg images within the android application using Picasso and Glide image loading library. If you want to load an SVG image within your application from the image URL we have to make an HTTP request. In this
4 min read
Play Audio From URL in Android using Kotlin
Many applications want to add different types of audio files to their android applications. These audio files are played using a media player within the android application. We can play audio files within the android application from different sources by playing audio from a web URL or by simply adding an audio file within our android application.
4 min read
Play Video From URL in Android Using Kotlin
Many applications display video within their application to display video content. The videos are huge in size so it is not practically possible to add each video to our project. This will increase the size of our application. So for tackling this many applications plays the video from the URL within their android application. In this video, we wil
3 min read
How to Play Audio From URL in Android using Jetpack Compose?
Many apps require the feature to add the audio feature in their application and there are so many audio files that we have to play inside our application. If we will store so many audio files inside our application, then it will increase the size of the app and this may reduce the user base due to the huge app size. So the better way to tackle this
7 min read
How to Share Image From URL with Intent in Android?
In this article, we will see how can we share images and text with Android Intent. In this activity URL of an image to be shared will be given with extra text and we will open the Dialog where the user chooses using which he wants to share this image as shown on the screen. A sample video is given below to get an idea about what we are going to do
5 min read
How to Play Video from URL in Android?
In this article, you will see how to play a video from a URL on Android. For showing the video in our Android application we will use the VideoView widget. The VideoView widget is capable of playing media files, and the formats supported by the VideoView are 3gp and MP4. By using VideoView you can play media files from the local storage and also fr
3 min read
Article Tags :
Practice Tags :