In this post we will see how to use browser intent.
In app if you want to open url i.e. website in phones browser.
then Browser Intent is used.
the link will open in Phone’s default browser.
if user installed other browser app
then it will show list of browser to select one of them.
lets get started .
1. open android studio
2. create a new project.
3. Set application name Browser Intent
4. I used package path as com.androidcodr.browserintent click next.
5. On this screen set minimum sdk API 16 click next.
6. Add empty activity. click next.
7. keep Activity name as MainActivity click finish. wait till project load completely.
8. Goto activity_main.xml
9. if you are having constrainLayout change it to RelativeLayout
10. add a button at center of screen.
set its properties as follows.
text=Open google
padding=20dp
textSize=16sp
onClick=openurl
layout_margin=20dp
id=@+id/mainbtnopen
Android Browser Intent |
<Button android:text="Open google" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:id="@+id/mainbtnopen" />
11. change property of helloworld textview
text=”Click button below to open www.google.com”
textSize=”18sp”
layout_above=”@+id/mainbtnopen”
gravity=”center_horizontal”
layout_margin=”10dp”
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Click button below to open www.google.com" android:textSize="18sp" android:layout_above="@+id/mainbtnopen" android:gravity="center_horizontal" android:layout_margin="10dp" />
12. open MainActivity.java
add method openurl for button
public void openurl(View view) { }
13. define proper url in String
lets open google
String url = "http://www.google.com";
13. define new Intent which will open our url.
ACTION_VIEW
Intent i = new Intent(Intent.ACTION_VIEW);
14. use setdata method to pass Uri.
parse string to it.
i.setData(Uri.parse(url));
15. finally startintent
startActivity(i);
16.so your full method will look like this.
public void openurl(View view) { String url = "http://www.google.com"; Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i); }
17. Build your Project and run.
now click on button open google.
Android Browser Intent |
18. Here in my mobile 3 browser app installed.
so its showing me list to select in which one url should
open.
Android Browser Intent |
19. I selected chrome.
Android Browser Intent |
20. url is now opened.
21. if you dont want button. you can use simple textview.
to look textview is clickable we will draw under line to text.
22. first add Textview below our button.
set its properties as follows
text=”Yahoo”
layout_below=”@+id/mainbtnopen”
id=”@+id/maintvyahoo”
OnClick=”openyahoo”
textSize=”18sp”
<TextView android:text="Yahoo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/mainbtnopen" android:layout_centerHorizontal="true" android:id="@+id/maintvyahoo" android:onClick="openyahoo" android:textSize="18sp" />
23. now declare field textview in MainActivity.
TextView tvyahoo;
24. in method OnCreate initialize textview.
tvyahoo = (TextView)findViewById(R.id.maintvyahoo);
25. now we will use sentPaintFlag method to draw under line.
tvyahoo.setPaintFlags(tvyahoo.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
26. now add method openyahoo
just like we added for button.
change url to http://www.yahoo.com
here is method.
public void openyahoo(View view) { String url = "http://www.yahoo.com"; Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i); }
27. again build project and see.
yahoo textview is having underline.
Android Browser Intent |
28. click on text yahoo
it will open url in browser.
Android Browser Intent |
29. play with code change url.
and check result.
Here is full code of activity_main.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:id="@+id/activity_main" 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.androidcodr.browserintent.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Click button below to open www.google.com" android:textSize="18sp" android:layout_above="@+id/mainbtnopen" android:gravity="center_horizontal" android:layout_margin="10dp" /> <Button android:text="Open google" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:onClick="openurl" android:layout_centerHorizontal="true" android:id="@+id/mainbtnopen" /> <TextView android:text="Yahoo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/mainbtnopen" android:layout_centerHorizontal="true" android:id="@+id/maintvyahoo" android:onClick="openyahoo" android:textSize="18sp" /> </RelativeLayout>
Here is full code of MainActivity
package com.androidcodr.browserintent; import android.content.Intent; import android.graphics.Paint; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView tvyahoo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvyahoo = (TextView)findViewById(R.id.maintvyahoo); tvyahoo.setPaintFlags(tvyahoo.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); } public void openurl(View view) { String url = "http://www.google.com"; Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i); } public void openyahoo(View view) { String url = "http://www.yahoo.com"; Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i); } }
Thanks.
If you Like post. Share with your Friends.
Subscribe us.
Follow us on Facebook Twitter and Google+
Appreciate it for this howling post, I am glad I observed this internet site on yahoo.