Open Website Url in Android’s Web browser from application

Use intent to send Website URL to android’s default web browser on button click through activity.

Intent are basically used to send application user to current activity to destination activity. But there are lot’s of other uses of intent. Android application developer can insert an particular website URL into android activity on button click using Intent and when user clicks on button then it will automatically open given url into built in default mobile device web browser. So here is the complete step by step tutorial for Open Website Url in Android’s Web browser from application.

android-project-download-code-button

How to Open Website Url in Android’s Web browser from application.

Code for MainActivity.java file.

package com.openurlandroidwebbrowser;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

 Button UrlOpen;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 UrlOpen = (Button)findViewById(R.id.button1);
 
 UrlOpen.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 Intent Getintent = new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.android-examples.com"));
 startActivity(Getintent);
 
 }
 });
 
 }
}

Code for activity_main.xml layout file.

 <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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.openurlandroidwebbrowser.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Click Here to Open URL in Android’s Web Browser" />

</RelativeLayout>

Screenshots:

Open Website Url in Android's Web browser from application

open url

Click here to download Open Website Url in Android’s Web browser from application project.

Leave a Reply

Your email address will not be published. Required fields are marked *