1. public class MsgcontactActivity extends Activity {
  2.  
  3. private static final int CONTACT_PICKER_RESULT = 1001;
  4. public void doLaunchContactPicker(View view)
  5. {
  6. Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
  7. startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
  8. }
  9.  
  10. EditText phoneTxt;
  11. TextView tv;
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.main);
  16. tv=(TextView) findViewById(R.id.textView1);
  17. String xx=phoneTxt.getText().toString();
  18. Toast.makeText(getApplicationContext(), xx,Toast.LENGTH_LONG ).show();
  19. }
  20.  
  21. @Override
  22. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  23. {
  24. String phone="";
  25. Cursor contacts=null;
  26. try
  27. {
  28. if (resultCode == RESULT_OK)
  29. {
  30. switch (requestCode)
  31. {
  32. case CONTACT_PICKER_RESULT:
  33. Uri result = data.getData();
  34. String id = result.getLastPathSegment();
  35. contacts=getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null);
  36. int phoneIdx = contacts.getColumnIndex(Phone.DATA);
  37. if (contacts.moveToFirst())
  38. {
  39. phone = contacts.getString(phoneIdx);
  40. EditText phoneTxt=(EditText)findViewById(R.id.phoneno);
  41. phoneTxt.setText(phone);
  42. }
  43. else
  44. {
  45. Toast.makeText(this, "error", 100).show();
  46. }
  47. break;
  48. }
  49. }
  50. else
  51. {
  52. Toast.makeText(this, "Warning: activity result not ok",50).show();
  53. }
  54. }
  55. catch (Exception e)
  56. {
  57. Toast.makeText(this, e.getMessage(), 50).show();
  58. }
  59. finally
  60. {
  61. if (contacts != null)
  62. {
  63. contacts.close();
  64. }
  65. }
  66. }
  67. }
  68.  
  69. <?xml version="1.0" encoding="utf-8"?>
  70. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  71. android:id="@+id/relativeLayout3"
  72. android:layout_width="match_parent"
  73. android:layout_height="wrap_content" >
  74.  
  75. <EditText
  76. android:layout_height="wrap_content"
  77. android:id="@+id/phoneno"
  78. android:inputType="text|phone"
  79. android:layout_width="wrap_content"
  80. android:layout_toLeftOf="@+id/Contacts"
  81. android:layout_alignParentLeft="true"/>
  82. <Button
  83. android:layout_width="wrap_content"
  84. android:layout_height="wrap_content"
  85. android:id="@+id/Contacts"
  86. android:text="Contacts"
  87. android:layout_alignParentRight="true"
  88. android:onClick="doLaunchContactPicker"/>
  89. <TextView
  90. android:text="TextView"
  91. android:layout_width="wrap_content"
  92. android:id="@+id/textView1"
  93. android:layout_height="wrap_content"
  94. android:layout_below="@+id/phoneno"
  95. android:layout_alignParentLeft="true"/>