Show selected contact phone number and name in text view - android

I write some code for get phone number and name of user from contacts,but I found some problem in my code the name of user show in text view but number not show ,pleas help me - my code is as follows-
public class MainActivity extends Activity {
public static final int PICK_CONTACT = 1;
private Button btnContacts;
private TextView txtContacts1;
private TextView txtContacts2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnContacts = (Button) findViewById(R.id.btn_contacts);
txtContacts1 = (TextView) findViewById(R.id.txt_contacts_name);
txtContacts2 = (TextView) findViewById(R.id.txt_contacts_number);
btnContacts.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_PICK,
People.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
}
#Override
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 = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name =c.getString(c.getColumnIndexOrThrow(People.NAME))+" : "+c.getInt(c.getColumnIndexOrThrow(People.NUMBER));
txtContacts1.setText(name);
}
}
break;
}
}
}
Thanks ..

People api for accessing contacts as been deprecated. You should use ContactsContract.
I've modified your code and it is working fine now. Here it is.
public class MainActivity extends Activity {
public static final int PICK_CONTACT = 1;
private Button btnContacts;
private TextView txtContacts1;
private TextView txtContacts2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnContacts = (Button) findViewById(R.id.btn_contacts);
txtContacts1 = (TextView) findViewById(R.id.txt_contacts_name);
btnContacts.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, PICK_CONTACT);
}
}
});
}
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
String cNumber = null;
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
null, null);
phones.moveToFirst();
cNumber = phones.getString(phones.getColumnIndex("data1"));
//System.out.println("number is:" + cNumber);
}
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
txtContacts1.setText(name + " : " + cNumber);
}
}
break;
}
}
}

Related

selection of different/multiple contact numbers

my code below is working fine...but it gives me only one contact number upon selection whereas i want to select different / multiple contact numbers and selected contacts to be shown on the screen...
have tried loops and all but not getting where exactly i am doing wrong so as not to achieve what i require....
can someone help me with this ??
public class Main2Activity extends Activity {
private static final int RESULT_PICK_CONTACT = 85500;
private TextView textView1;
private TextView textView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView1 = findViewById(R.id.textView1);
textView2 = findViewById(R.id.textView2);
}
public void selectContact(View v)
{
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case RESULT_PICK_CONTACT:
contactPicked(data);
break;
}
} else {
Log.e("Main2Activity", "Failed to pick contact");
}
}
private void contactPicked(Intent data) {
Cursor cursor = null;
try {
String phoneNo = null ;
String name = null;
Uri uri = data.getData();
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
phoneNo = cursor.getString(phoneIndex);
name = cursor.getString(nameIndex);
textView1.setText(name);
textView2.setText(phoneNo);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Get PicturePath selected from gallery to store it in dataBase

I used this code to upload image from gallery .it works perfectly..but the problem is that i wanna get the Image's path to store it in wamp dataBase ..`
public class Image extends Activity {
ImageView contact;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
contact = (ImageView) findViewById(R.id.candidat);
contact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Contact Image"), 1);
}
});
}
public void onActivityResult(int reqCode, int resCode, Intent data) {
if (resCode == RESULT_OK) {
if (reqCode == 1)
contact.setImageURI(data.getData());
}
}
}
Thank you for help
You can get the exact path using this method -
public String getPathFromURI(Uri contentURI) {
String result = null;
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int _id = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(_id);
cursor.close();
}
return result;
}
And use it like -
public void onActivityResult(int reqCode, int resCode, Intent data) {
if (resCode == RESULT_OK) {
if (reqCode == 1)
contact.setImageURI(data.getData());
String path = getPathFromURI(data.getData());
}
}
put this code:
public static final int IMAGEM = 1;
In the click put:
startActivityForResult(Intent.createChooser(new Intent(Intent.ACTION_PICK).setType("image/*"), "Select a image"), IMAGE);
In your activity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
try {
if (resultCode == RESULT_OK && requestCode == IMAGE){
String pathImg = getRealPathFromURI(intent.getData());
}
}catch (Exception e){
e.printStackTrace();
}
}
public String getRealPathFromURI(Uri uri) {
//this method work for any api
Cursor cursor = null;
try {
Uri newUri = handleImageUri(uri);
String[] proj = { MediaStore.Images.Media.DATA };
cursor = getContentResolver().query(newUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch (Exception e){
return null;
} finally {
if (cursor != null) {
cursor.close();
}
}
}

how to access phone book contacts?

I am using an Edittext and a button . On press of a button , phone book gets open and then user will select a contact from it and the selected phonenumber will get display on the edittext.
I followed many tutorials and but the methods that they are showing are already depreciated.
I have declared this permission: READ_CONTACTS in manifest
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_picker);
// this opens the activity. note the Intent.ACTION_GET_CONTENT
// and the intent.setType
((Button)findViewById(R.id.pick_person)).setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
// user BoD suggests using Intent.ACTION_PICK instead of .ACTION_GET_CONTENT to avoid the chooser
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// BoD con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver().query(uri, new String[]{
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE },
null, null, null);
if (c != null && c.moveToFirst()) {
String number = c.getString(0);
int type = c.getInt(1);
showSelectedNumber(type, number);
}
} finally {
if (c != null) {
c.close();
}
}
}
}
}
public void showSelectedNumber(int type, String number) {
Toast.makeText(this, type + ": " + number, Toast.LENGTH_LONG).show();
}
Answer provided by Digvesh Patel is correct. He has used "type" of contact, which returns number. so I have used his code and made some changes which i used in my application. it maybe helpful to someone
public int REQUESTCODE=1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button select = (Button) findViewById(R.id.select);
select.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, REQUESTCODE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
Log.i("data", uri.toString());
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver().query(uri, new String[]{
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE },
null, null, null);
if (c != null && c.moveToFirst()) {
String name = c.getString(0);
String number = c.getString(1);
int type = c.getInt(2);
showSelectedNumber(name, number,type);
}
} finally {
if (c != null) {
c.close();
}
}
}
}
}
public void showSelectedNumber(String name, String number, int type){
TextView usernumber = (TextView) findViewById(R.id.textView1);
String typelabel = (String) ContactsContract.CommonDataKinds.Phone.getTypeLabel(getResources(), type, "");
usernumber.setText(name+": "+number+" "+typelabel);
}

Details not coming properly by using Intent?

The details of the contacts not displayed properly by using this code. I am trying to access the Name, Number and Email ID by using the Intent. Name and Number only displayed when I click the button but not the Email.No error in the project.It's working good.My xml file have only one Button.
public class GetDetails extends Activity {
/** Called when the activity is first created. */
private static final int CONTACT_PICKER_RESULT = 1001;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button Btn = (Button)findViewById(R.id.getContacts);
Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(i, CONTACT_PICKER_RESULT);
}
});
}
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if(resultCode == RESULT_OK) {
switch (reqCode) {
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
Cursor emails = null;
String number = "";
String emailID = "";
try {
Uri result = data.getData();
//get the id from the uri
String id = result.getLastPathSegment();
//query
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone._ID + " = ? " , new String[] {id}, null);
int numberIdx = cursor.getColumnIndex(Phone.DATA);
if(cursor.moveToFirst()) {
number = cursor.getString(numberIdx);
} else {
}
emails = getContentResolver().query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
int num = emails.getColumnIndex(Email.DATA);
if(emails.moveToFirst()) {
emailID = emails.getString(num);
} else {
}
} catch (Exception e) {
//failed
} finally {
if (cursor!=null) {
cursor.close();
}
}
}
}
}
}
How to solve this situation ?
Replace this line:
emails = getContentResolver().query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
with this line :
emails = getContentResolver().query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = ?", new String[]{id}, null);

How to get the phone number?

I'm trying to put in an edittext the phone number that was selected by user. I have a button :-
contact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK,
Phone.CONTENT_URI);
startActivityForResult(intent, 0);
}
});
and the function below :
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case 0:
if (resultCode == Activity.RESULT_OK) {
Cursor c = getContentResolver().query(Phone.CONTENT_URI, null,
null, null, null);
c.moveToFirst();
String phone = c.getString(c.getColumnIndexOrThrow(Phone.NUMBER));
phone = phone.replace("-", "");
Log.v("getting phone number", "Phone Number: " + phone);
txtPhoneNo.setText(phone);
}
break;
}
}
and I got the phone number of last contact. How can I take the phone number that was selected?
Do something like:
if (resultCode == Activity.RESULT_OK)
{
Uri contactData = data.getData();
ContentResolver cr = getContentResolver();
Cursor curContact = managedQuery(contactData, null, null, null, null);
if ((curContact != null) && (curContact.moveToFirst()))
{
String id = curContact.getString(curContact
.getColumnIndexOrThrow(BaseColumns._ID));
// check there's a phone number at all
if (Integer.parseInt(curContact.getString(curContact
.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
// get the phone number
Cursor curNumbers = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[] { id }, null);
if ((curNumbers != null) && curNumbers.moveToFirst())
{
String strNumber =
curNumbers.getString(
curNumbers.getColumnIndexOrThrow(
ContactsContract.CommonDataKinds.Phone.NUMBER)));
curNumbers.close();
}
}
curContact.close();
}
Have a look at this code!
btn_existing_contacts.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent intent_contacts = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
/*contacts.setAction(android.content.Intent.ACTION_VIEW);
contacts.setData(People.CONTENT_URI);*/
startActivityForResult(intent_contacts, 0);
//displayContacts();
}
});
after returning from contacts screen,
#Override
protected void onActivityResult(int requestCode, int resultCode,Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
String name,mailid,id;
switch(requestCode)
{
case 0:
{
if(resultCode == RESULT_OK )
{
Uri contactdata = data.getData();
Cursor cur = managedQuery(contactdata, null, null, null, null);
if(cur.moveToFirst())
{
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor emailCur = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",new String[]{id}, null);
emailCur.moveToFirst();
String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
name = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
//mailid = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
//mailid = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Email._ID));
//Toast.makeText(context_contact, "Name:"+name+"\nmailid:"+email, Toast.LENGTH_SHORT).show();
Intent intent_add_invitees = new Intent(<ClassContext>,<ur classname>.class);
intent_add_invitees.putExtra("invitee_name", name);
intent_add_invitees.putExtra("invitee_mailid", email);
setResult(RESULT_OK, intent_add_invitees);
finish();
}
}
}
}
}

Categories

Resources