-
-
Save jonikarppinen/f6324fceed582b84aa2a51c1a3fbad20 to your computer and use it in GitHub Desktop.
Example of choosing a contact with phone number in Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.design.widget.CoordinatorLayout | |
android:id="@+id/coordinator_layout" | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
> | |
<RelativeLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<Button | |
android:id="@+id/contacts_button" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerInParent="true" | |
android:text="Open contacts" | |
tools:ignore="HardcodedText"/> | |
</RelativeLayout> | |
</android.support.design.widget.CoordinatorLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.content.Intent; | |
import android.database.Cursor; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.provider.ContactsContract; | |
import android.support.design.widget.CoordinatorLayout; | |
import android.support.design.widget.Snackbar; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import butterknife.BindView; | |
import butterknife.ButterKnife; | |
import butterknife.OnClick; | |
/** | |
* Minimal example of choosing a contact (with phone number), and doing something with the basic | |
* data: contact id, display name, selected phone number (in this case just show it in Snackbar) | |
* | |
* Targetting Android 6, supporting 4.1 (minSdkVersion 16). | |
* | |
* @author Joni Karppinen | |
*/ | |
public class ContactsTestActivity extends AppCompatActivity { | |
@BindView(android.R.id.content) | |
View contentView; | |
@OnClick(R.id.contacts_button) | |
public void onContactsClicked() { | |
pickContact(); | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_contacts_test); | |
ButterKnife.bind(this); | |
} | |
private final static int PICK_CONTACT = 0; | |
private void pickContact() { | |
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); | |
// Show only contacts with phone number. (This also forces user to choose ONE phone number | |
// for a contact that has several. In addition it simplifies our code in | |
// handleSelectedContact() as various checks become unnecessary.) | |
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); | |
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) { | |
handleSelectedContact(data); | |
} | |
break; | |
} | |
} | |
private void handleSelectedContact(Intent intent) { | |
Uri contactUri = intent.getData(); // uri for the picked contact | |
Cursor c = getContentResolver().query(contactUri, null, null, null, null); | |
if (c.moveToFirst()) { | |
String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID)); | |
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); | |
String selectedPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.Entity.DATA1)); | |
String message = String.format("%s has id %s, selected phone %s", name, id, selectedPhone); | |
Snackbar.make(contentView, message, Snackbar.LENGTH_LONG).show(); | |
} | |
c.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment