Pick a Number and Name From Contacts List in android app
Try following code it will help you:
// You need below permission to read contacts
<uses-permission android:name="android.permission.READ_CONTACTS"/>
// Declare
static final int PICK_CONTACT=1;
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
//code
@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 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));
}
}
break;
}
}
Picking contacts number from the contact list and putting it in an EditText
If you specifically need a phone number to be picked by the user, you can request for a phone-number-picker, instead of a contact-picker like you're doing.
Try this:
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
The intent data you'll get in return would be for a specific phone row in the Data table:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if ((requestCode == 1) && (resultCode == RESULT_OK)) {
Cursor cursor = null;
try {
Uri uri = data.getData();
cursor = getContentResolver().query(uri, new String[] { CommonDataKinds.Phone.NUMBER }, null, null, null);
if (cursor != null && cursor.moveToNext()) {
String phone = cursor.getString(0);
// Do something with phone
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
pick up number and name from contact list
This is not complete solution, but I hope it'll be some what useful.
Suppose my button clicks are as below
btncontact.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i,1);//Request_code is 1
}
});
findViewById(R.id.button5).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(Intent.ACTION_PICK);
i.setType("image/*");
startActivityForResult(i,2);//Request_code is 2
}
});
Now, for onActivityResult,
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(data!=null && data.getData()!=null){
Uri _uri=data.getData();
Log.d("Nzm", ""+_uri);
switch(requestCode){
case 1:
// you have _uri in your hand which have contact id. Do necessary steps for contact as you want here.
break;
case 2:// Action for your image
Intent i=new Intent(Intent.ACTION_VIEW);
i.setData(_uri);
startActivity(i);
break;
}
}
}
Related Topics
Android Studio Create Project Template for New Projects
How to Have a Viewpager Inside of a Scrollview
Disable Soft-Keyboard from Edittext But Still Allow Copy/Paste
Require a Password to Uninstall/Remove Application
How to Know When Tts Is Finished
Speed Control of Mediaplayer in Android
Android/Java: Transition/Migration from Proguard to R8
How to Make My Own Custom Dialer in an Android Phone
Android Xml Rounded Clipped Corners
How to Change Package Name in Android Studio
Can Android's Webview Automatically Resize Huge Images
How to Create Always-Top Fullscreen Overlay Activity in Android
Xiaomi Does Not Receive Notification When Application Is Not Running
How to Take Multiple Photos Before Dismissing Camera Intent