How to display a phone number by pressing the add button in android

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

You are reading an archived discussion.

Related Posts

"Good morning to every one of you people on Earth. I am robot astronaut Kirobo. I am the world's first talking robot astronaut. Nice to meet you," blabbers the world's...
hi friends .. can you say me the website where i can get the definitons of electrical and electronic components and also the definitons in power systems....
I am in trouble to decide which Laptop to purchase. I have the following specifications in my mind Minimum 6GB RAM 750GB HDD DVD R/RW drive Any processor Graphics (not...
Our friends at Texas Instruments (TI) have organised the second edition of MCU Deep Dive events in Pune, Bangalore and New Delhi. The events are aimed at educating the modern...
Seagate Technology plc (NASDAQ: STX) has announced that they're shipping world's thinnest HDD with 2TB capacity - the Spinpoint™ M9T. It's just 9.5 mm thick thin and is available in...