Launching the Browser From Your Android Applications - The Easy Way
This quick tip shows you how to launch the built-in Browser application in three ways. First, you learn how to launch the browser to a specific URL. Second, you learn how to create text with links. Third, you learn how to launch a Google web search and specify the search criteria. You will achieve these goals by creating and configuring the appropriate Intents within your application’s Activity class.
Step 1: Create an Android Application
Begin by creating an Android project. Implement your Android application as normal. Once you have a project set up and the application running, decide under what circumstances you want to launch the browser. Will this occur when Button controls are pressed? Implement the necessary controls that will trigger to web browsing or searching features of the application, including any click handling. Once you have completed these tasks, you have places to drop in the code to launch the browser or web search. Now you are ready to proceed with this quick tip.
You can follow along with our project: HelloWorldWideWeb, which is available as open source.
Step 2: Working with URIs
Android uses Uri (Uniform Resource Identifier) objects to identify the unique location of a piece of data. Uri objects are often used to specify the data that an Intent is supposed to use. In this case, we will create a Uri object from a web URL using the parse() method:
1 |
|
2 |
Uri uriUrl = Uri.parse("http://androidbook.blogspot.com/"); |
Step 3: Creating the Intent
You can view HTML content using the following Intent: android.content.Intent.ACTION_VIEW. Begin by creating an Intent of this type and specifying the URI you created above, as follows, within your Button click handler:
1 |
|
2 |
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl); |
Step 4: Launching the Intent
When you launch this Intent, any applications that can display web will be able to handle this request. Once you have set the type data for the Intent, you can call the startActivity() method, passing in your Intent:
1 |
|
2 |
startActivity(launchBrowser); |
When you click on this button, the Browser application (which generally handles HTML content display) is launched to the website you provided.



When you hit the back button, you return to the previous Activity, which happens to be your application.
Step 5: Using Links in Text
Another easy way to launch in to the browser is simply by including links within text on the screen. The TextView object can be configured to find these and turn then in to clickable links, like in a web browser, such that when the user clicks on them they launch the browser to the appropriate spot. For instance, the following TextView does just that:
1 |
|
2 |
<TextView
|
3 |
android:layout_width="fill_parent" |
4 |
android:layout_height="wrap_content" |
5 |
android:text="@string/contains_links" |
6 |
android:textSize="14dp" |
7 |
android:autoLink="web" /> |
The following screenshot shows what this looks like.



The text for @string/contains_links is verbatim for what you see on the screen. No special formatting commands or tags are needed within the string.
Step 6: Enabling Web Searches
When you want to provide the user with the ability to perform a web search, you could still use the ACTION_VIEW intent and set up the query strings appropriate to a specific search engine, or if you are content with a Google search, you can simply use the web search Intent: android.content.Intent.ACTION_WEB_SEARCH. Begin by creating an Intent of this type, as follows, within your second Button click handler:
1 |
|
2 |
Intent search = new Intent(Intent.ACTION_WEB_SEARCH); |
Step 7: Supplying Search Criteria
Often, you want to supply some criteria to search on. You can do this by supplying this information as part of the Intent’s extras. The ACTION_WEB_SEARCH Intent specifically uses the SearchManager.QUERY extra field for the search criteria. For example, to perform the Google search on pygmy goats, you configure the SearchManager.QUERY extra and launch the Browser as follows:
1 |
|
2 |
Intent search = new Intent(Intent.ACTION_WEB_SEARCH); |
3 |
search.putExtra(SearchManager.QUERY, "pygmy goats"); |
4 |
startActivity(search); |
When you click on this button, the Browser application (which generally handles HTML content display) is launched to the website you provided.



A Note on Permissions: Although your application is leveraging browser capabilities on the device, it is not required to have any such permissions. This is because the application is not directly displaying web content. Instead, it’s just leveraging other applications’ capabilities to do so.
Becoming a Browser
For more fine control over web content within your applications, you’ll want to use the WebView control. This special view allows fine control over rendering of web content. However, this control will require your application to have the appropriate permissions to do so, and that, friends, is discussed in a tutorial right here on Mobiletuts+!
Conclusion
In this quick tip you have learned how to configure an Intent to launch the Browser as well as perform a search query. This feature can be very useful for applications wishing to web content within their applications.
About the Authors
Mobile developers Lauren Darcey and Shane Conder have coauthored several books on Android development: an in-depth programming book entitled Android Wireless Application Development and Sams TeachYourself Android Application Development in 24 Hours. When not writing, they spend their time developing mobile software at their company and providing consulting services. They can be reached at via email to androidwirelessdev+mt@gmail.com, via their blog at androidbook.blogspot.com, and on Twitter @androidwireless.