CrazyEngineers
  • here is my java code
    package com.example.test;

    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    import android.provider.ContactsContract.Contacts;
    import android.app.Activity;
    import android.content.Intent;
    import android.database.Cursor;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    public class MainActivity extends Activity {

    @SuppressWarnings("unused")
    private static final int PICK_CONTACT_REQUEST = 0;

    private static final int PICK_CONTACT = 1;
    Button pickContact;
    EditText pc;
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    pickContact = (Button) findViewById(R.id.button1);
    pc = (EditText) findViewById(R.id.editText1);
    pickContact.setOnClickListener(new OnClickListener()
    {
    public void onClick(View arg0) {

    Intent intent1 = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
    startActivityForResult(intent1, PICK_CONTACT);
    startActivity(intent1);
    }});

    }
    public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode) {
    case (PICK_CONTACT) :
    if (resultCode == Activity.RESULT_OK) {
    Uri contactData = data.getData();
    Cursor c = getContentResolver().query(contactData, null, null, null, null);
    if (c.moveToPosition(PICK_CONTACT)) {
    String name=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
    String number=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    Toast.makeText(this, name + " has number " + number, Toast.LENGTH_LONG).show();
    pc.setText(number);
    pc.setText(name);
    }
    }
    break;
    } } }
    by using this code i am able to access the contact list bt when i click on the contact its not showing into my editText box
    here is my XML code
    xmlns:tools="#-Link-Snipped-#"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="@string/Add" />

    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/button1"
    android:ems="10"
    android:inputType="phone"
    android:text="@string/pc" >




    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="100dp"
    android:layout_toLeftOf="@+id/button1"
    android:ems="10"
    android:inputType="textMultiLine" />

    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/editText2"
    android:layout_alignLeft="@+id/button1"
    android:layout_alignParentRight="true"
    android:text="@string/SMS" />



    Thank you 😀
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Home Channels Search Login Register