Browsing by Intent

The simplest thing you can do with Android's networking API is to open a browser on a web page of your choice. You might want to do this to provide a link to your home page from your program or to access some server-based application such as an ordering system. In Android all it takes is three lines of code.

To demonstrate, let's write a new example called BrowserIntent, which will have an edit field where you can enter a URL and a Go button you press to open the browser on that URL (see Figure 7.1). Start by creating a new "Hello, Android" project with the following values in the New Project wizard:

Project name: BrowserIntent Build Target: Android 2.2 Application name: BrowserIntent Package name: org.example.browserintent Create Activity: BrowserIntent Min SDK Version: 8

Once you have a the basic program, change the layout file (res/layout/ main.xml) so it looks like this:

Download Browserlntent/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation= "horizontal" android:layout_width= "fill_parent" android:layout_height="fill_parent"> <EditText android:id= "@+id/url_field" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0" android:lines="1" android:inputType="textUri" android:imeOptions="actionGo" /> <Button android:id="@+id/go_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/go_button" /> </LinearLayout>

This defines our two controls, an EditText control and a Button.

On EditText, we set android:layout_weight="1.0" to make the text area fill up all the horizontal space to the left of the button, and we also set android:lines="1" to limit the height of the control to one vertical line. Note that this has no effect on the amount of text the user can enter here, just the way it is displayed.

Android 1.5 introduced support for soft keyboards and other alternate input methods. The options for android:inputType="textUri" and android:imeOptions="actionGo" are hints for how the soft keyboard should appear. They tell Android to replace the standard keyboard with one that has convenient buttons for ".com" and "/" to enter web addresses and has a Go button that opens the web page.4

As always, human-readable text should be put in a resource file, res/ values/strings.xml.

4. See http://d.android.com/reference/android/widget/TextView.html and

http://android-developers.blogspot.com/2009/04/updating-applications-for-on-screen.html for more information on input options.

Download BrowserIntent/res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?> <resources>

<string name="app_name">BrowserIntent</string> <string name="go_button">Go</string> </resources>

Next we need to fill in the onCreate() method in the BrowserIntent class. This is where we'll build the user interface and hook up all the behavior. If you don't feel like typing all this in, the complete source code is available online at the book's website.5

Download BrowserIntent/src/org/example/browserintent/BrowserIntent.java

Line 1 package org.example.browserintent;

- import android.app.Activity;

- import android.content.Intent; 5 import android.net.Uri;

- import android.os.Bundle;

- import android.view.KeyEvent;

- import android.view.View;

- import android.view.View.OnClickListener; 10 import android.view.View.OnKeyListener;

- import android.widget.Button;

- import android.widget.EditText;

- public class BrowserIntent extends Activity { 15 private EditText urlText;

private Button goButton;

- @Override public void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState);

setContentView(R.layout.main);

- // Get a handle to all user interface elements urlText = (EditText) findViewById(R.id.url_field);

25 goButton = (Button) findViewById(R.id.go_button);

- // Setup event handlers

- goButton.setOnClickListener(new OnClickListener() {

- public void onClick(View view) {

30 openBrowser();

urlText.setOnKeyListener(new OnKeyListener() { - public boolean onKey(View view, int keyCode, KeyEvent event) {

5. http://pragprog.com/titles/eband3

35 if (keyCode == KeyEvent.KEYCODE_ENTER) {

- openBrowser(); return true;

return false;

Inside onCreate(), we call setContentView() on line 21 to load the view from its definition in the layout resource, and then we call findViewById() on line 24 to get a handle to our two user interface controls.

Line 28 tells Android to run some code when the user selects the Go button, either by touching it or by navigating to it and pressing the center D-pad button. When that happens, we call the openBrowser() method, which will be defined in a moment.

As a convenience, if the user types an address and hits the Enter key (if their phone has one), we want the browser to open just like they had clicked Go. To do this, we define a listener starting on line 33 that will be called every time the user types a keystroke into the edit field. If it's the Enter key, then we call the openBrowser() method to open the browser; otherwise, we return false to let the text control handle the key normally.

Now comes the part you've been waiting for: the openBrowser() method. As promised, it's three lines long:

Download BrowserIntent/src/org/example/browserintent/BrowserIntent.java

/** Open a browser on the URL specified in the text box */ private void openBrowser() {

Uri uri = Uri.parse(urlText.getText().toString()); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);

The first line retrieves the address of the web page as a string (for example, "http://www.android.com") and converts it to a uniform resource identifier (URI).

Note: Don't leave off the "http://" part of the URL when you try this. If you do, the program will crash because Android won't know how to handle the address. In a real program you could add that if the user omitted it.

Web Images local Mews more -

0 0

Post a comment