Select a Contact Example

In this example you'll create a new Activity that serviceACTION_PICK for contact data. It displays each of the contacts in the contacts database and lets the user select one, before closing and returning the selected contact's URI to the calling Activity.

It's worth noting that this example is somewhat contrived. Android already supplies an Intent Filter for picking a contact from a list that can be invoked by means of the content://contacts/people/ URI in an implicit Intent. The purpose of this exercise is to demonstrate the form, even if this particular implementation isn't particularly useful.

1. Create a new ContactPicker project that includes a ContactPicker Activity: package com.paad.contactpicker;

import android.app.Activity;

import android.content.Intent;

import android.database.Cursor;

import android.net.Uri;

import android.os.Bundle;

import android.provider.Contacts.People;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ListView;

import android.widget.SimpleCursorAdapter;

import android.widget.AdapterView.OnItemClickListener;

public class ContactPicker extends Activity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main);

2. Modify the main.xml layout resource to include a single ListView control. This control will be used to display the contacts.

<?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" >

<ListView android:id="@+id/contactListView" android:layout_width="fill_parent" android:layout_height="wrap_content"

</LinearLayout>

3. Create a new listitemlayout.xml layout resource that includes a single Textview. This will be used to display each contact in the List View.

<?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" >

<TextView android:id="@+id/itemTextView"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:padding="10px"

android:textSize="16px"

android:textColor="#FFF"

</LinearLayout>

4. Return to the ContactPicker Activity. Override the onCreate method and extract the data path from the calling Intent:

©Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main);

Intent intent = getIntent();

String dataPath = intent.getData().toString();

4.1. Create a new data URI for the people stored in the contact list, and bind it to the List View using a SimpleCursorArrayAdapter:

The SimpleCursorArrayAdapter lets you assign Cursor data, used by Content Providers, to Views. It's used here without further comment but is examined in detail later in this chapter.

final Uri data = Uri.parse(dataPath + "people/"); final Cursor c = managedQuery(data, null, null, null);

String[] from = new String[] {People.NAME}; int[] to = new int[] { R.id.itemTextView };

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,

R.layout.listitemlayout, c, from, to);

ListView lv = (ListView)findViewByld(R.id.contactListView); lv.setAdapter(adapter);

4.2. Add an onItemClickListener to the List View. Selecting a contact from the list should return a path to the item to the calling Activity.

lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {

// Move the cursor to the selected item c.moveToPosition(pos);

int rowId = c.getInt(c.getColumnIndexOrThrow("_id")); // Construct the result URI.

Uri outURI = Uri.parse(data.toString() + rowId); Intent outData = new Intent(); outData.setData(outURI); setResult(Activity.RESULT_OK, outData); finish();

4.3. Close off the onCreate method:

5. Modify the application manifest and replace the intent-filter tag of the Activity to add support for the action_pick action on contact data:

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.paad.contactpicker"> <application android:icon="@drawable/icon">

<activity android:name="ContactPicker" android:label="@string/app_name"> <intent-filter>

<action android:name="android.intent.action.PICK"></action> <category android:name="android.intent.category.DEFAULT"></category> <data android:path="contacts" android:scheme="content"></data> </intent-filter> </activity> </application> </manifest>

6. This completes the sub-Activity. To test it, create a new test harness ContentPickerTester Activity. Create a new layout resource — contentpickertester.xml —that includes a TextView to display the selected contact and a Button to start the sub-Activity:

<?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" >

<TextView android:id="@+id/selected_contact_textview"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

<Button android:id="@+id/pick_contact_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Pick Contact"

</LinearLayout>

7. Override the onCreate method of the ContentPickerTester to add a click listener to the Button so that it implicitly starts a new sub-Activity by specifying the action_pick and the contact database URI (content://contacts/):

package com.paad.contactpicker;

import android.app.Activity;

import android.content.Intent;

import android.database.Cursor;

import android.net.Uri;

import android.os.Bundle;

import android.provider.Contacts.People;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

public class ContentPickerTester extends Activity { public static final int PICK_CONTACT = 1;

@Override public void onCreate(Bundle icicle) { super.onCreate(icicle);

setContentView(R.layout.contentpickertester);

Button button = (Button)findViewById(R.id.pick_contact_button);

button.setOnClickListener(new OnClickListener() { @Override public void onClick(View _view) {

Intent intent = new Intent(Intent.ACTION_PICK,

Uri.parse("content://contacts/")); startActivityForResult(intent, PICK_CONTACT);

8. When the sub-Activity returns, use the result to populate the Text View with the selected contact's name:

@Override public void onActivityResult(int reqCode, int resCode, Intent data) { super.onActivityResult(reqCode, resCode, data);

switch(reqCode) {

if (resCode == Activity.RESULT_OK) { Uri contactData = data.getData();

Cursor c = managedQuery(contactData, null, null, null); c.moveToFirst();

String name = c.getString(c.getColumnIndexOrThrow(People.NAME)); TextView tv = (TextView)findViewById(R.id.selected_contact_textview); tv.setText(name);

break;

9. With your test harness complete, simply add it to your application manifest. You'll also need to add a READ_CONTACTS permission within a uses-permission tag, to allow the application to access the contacts database.

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.paad.contactpicker"> <application android:icon="@drawable/icon">

<activity android:name=".ContactPicker" android:label="@string/app_name"> <intent-filter>

<action android:name="android.intent.action.PICK"></action> <category android:name="android.intent.category.DEFAULT"></category> <data android:path="contacts" android:scheme="content"></data> </intent-filter> </activity>

<activity android:name=".ContentPickerTester" android:label="Contact Picker Test"> <intent-filter>

<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

</application>

<uses-permission android:name="android.permission.READ_CONTACTS"/>

</manifest>

All code snippets in this example are part of the Chapter 5 Contact Picker project, available for download at Wrox.com.

When your Activity is running, press the button. The contact picker Activity should be shown as in Figure 5-1.

Once you select a contact, the parent Activity should return to the foreground with the selected contact name displayed, as shown in Figure 5-2.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Responses

Post a comment