Contact list demo
package app.test;
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 Test extends Activity {
public static final int PICK_CONTACT = 1;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.pick_contact_button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View _view) {
Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts/"));
startActivityForResult(intent, PICK_CONTACT);
}
});
}
@Override
public void onActivityResult(int reqCode, int resCode, Intent data) {
super.onActivityResult(reqCode, resCode, data);
switch(reqCode) {
case (PICK_CONTACT) : {
if (resCode == Activity.RESULT_OK) {
Cursor c = managedQuery(data.getData(), null, 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;
}
}
}
}
//main.xml
<?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>
Related examples in the same category