Pick Em

Sometimes you know your content Uri represents a collection of some type, such as content:// contacts/people representing the list of contacts in the stock Android contacts list. In this case, you can let the user pick a contact that your activity can then use (e.g., tag it, dial it).

To do this, you need to create an Intent for the ACTION PICK on the target Uri, then start a sub-activity (via startActivityForResult()) to allow the user to pick a piece of content of the specified type. If your onActivityResult() callback for this request gets a RESULT OK result code, your data string can be parsed into a Uri representing the chosen piece of content.

For example, take a look at Introspection/Pick in the sample applications in the Source Code section of http://apress .com. This activity gives you a field for a collection Uri (with content://contacts/people pre-filled in for your convenience), plus a really big Gimme! button:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout width="fill parent"

android:layout height="fill parent" >

<EditText android:id="@+id/type" android:layout width="fill parent" android:layout height="wrap content" android:cursorVisible="true" android:editable="true" android:singleLine="true" android:text="content://contacts/people"

<Button android:id="@+id/pick" android:layout width="fill parent" android:layout height="fill parent" android:text="Gimme!" android:layout weight="1"


Upon being clicked, the button creates the ACTION PICK on the user-supplied collection Uri and starts the sub-activity. When that sub-activity completes with RESULT OK, the ACTION VIEW is invoked on the resulting content Uri.

public class PickDemo extends Activity { static final int PICK REQUEST=1337; private EditText type;

@Override public void onCreate(Bundle icicle) { super. onCreate(icicle); setContentView(R.layout.main); type=(EditText)findViewById(R.id.type);

Button btn=(Button)findViewById(R.id.pick);

btn.setOnClickListener(new View.OnClickListener() { public void onClick(View view) {

Intent i=new Intent(Intent.ACTION PICK,


startActivityForResult(i, PICK REQUEST);

@Override protected void onActivityResult(int requestCode, int resultCode,

Intent data) { if (requestCode==PICK REQUEST) { if (resultCode==RESULT OK) {

startActivity(new Intent(Intent.ACTION VIEW, data.getData()));

The result: the user chooses a collection (Figure 25-1), picks a piece of content (Figure 25-2), and views it (Figure 25-3).

Figure 25-1. The PickDemo sample application, as initially launched

Jane Smith

Mobile +1.202.555.1212

Joe Schmoe

Mobile +1.212.555.1212

Figure 25-2. The same application, after the user has clicked the Gimme! button, showing the list of available people

Figure 25-3. A view of a contact, launched by PickDemo after the user has chosen one of the people from the pick list

Was this article helpful?

0 0

Post a comment