Advertisement
Android SDK

Launching the Browser From Your Android Applications - The Easy Way

by

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:

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:

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:

startActivity(launchBrowser);

When you click on this button, the Browser application (which generally handles HTML content display) is launched to the website you provided.

Android WebView

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:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/contains_links"
    android:textSize="14dp"
    android:autoLink="web" />

The following screenshot shows what this looks like.

Open Android Browser

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:

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:

Intent search = new Intent(Intent.ACTION_WEB_SEARCH); 
search.putExtra(SearchManager.QUERY, "pygmy goats");
startActivity(search);

When you click on this button, the Browser application (which generally handles HTML content display) is launched to the website you provided.

Android WebView

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.

Need More Help Writing Android Apps? Check out our Latest Books and Resources!

Buy Android Wireless Application Development, 2nd Edition  Buy Sam's Teach Yourself Android Application Development in 24 Hours  Mamlambo code at Code Canyon

Related Posts
  • Code
    Android SDK
    Integrating Google Play Services on Android85ude preview image@2x
    By integrating your Android apps with Google Play Services, you can access Google services, such as Maps, Drive, and Google+. In this tutorial, we will go through the process of integrating Google Play Services with Android apps.Read More…
  • Code
    Android SDK
    Using New Relic to Monitor Your Android AppGetting started new relic retina preview2
    In the last two years, New Relic has focused hard on building out a solution for monitoring the performance of mobile apps. In this tutorial, we will look at how you can start using New Relic to monitor the performance of an Android application.Read More…
  • Code
    Android SDK
    Create a Music Player on Android: Project Setup0d63m preview image@2x
    The Android platform provides resources for handling media playback, which your apps can use to create an interface between the user and their music files. In this tutorial series, we will create a basic music player application for Android. The app will present a list of songs on the user device, so that the user can select songs to play. The app will also present controls for interacting with playback and will continue playing when the user moves away from the app, with a notification displayed while playback elapses.Read More…
  • Code
    Android SDK
    Android SDK: Next StepsAndroid preview@2x
    In this series, we've begun learning how to develop Android applications from scratch. We started exploring the development tools, got acquainted with the basic elements in an application project, looked at user interface design, interactivity, resources, and data, and we've also took a closer look at what happens when your application is running. What we've covered so far should put you in a good position to get started creating functional Android applications, but Android has a lot more to offer so the range of possibilities is virtually endless. You may therefore struggle to choose what to learn next. In this part, we'll wrap up the series by pointing out some possible directions for future learning. After this, the final part will be a quiz on what we covered throughout the series.Read More…
  • Code
    Android SDK
    Android SDK: Common Android ComponentsAndroid preview@2x
    In this series we are learning the essential features of Android development that you need to know to start building apps. So far, we've looked at the structure and typical elements in an Android app, including user interface elements and data storage. You can use what we covered already to start creating your own apps. But before you do, we will run through some common Android components in this tutorial, then have a quick look at the SDK samples in the next tutorial.Read More…
  • Code
    Android SDK
    Android SDK: Virtual & Physical DevicesAndroid preview@2x
    We are learning about Android app development in this series. We already looked at the basic elements in an Android project and touched on user interface design and data storage along the way. In this tutorial, we are going to explore how to run and interact with your apps on physical and virtual devices. In the next part of the series, we will work through the process of running an app on a physical device and on the emulator. In this part, we'll look at getting our physical and virtual devices set up in Eclipse.Read More…