Skip to content

Instantly share code, notes, and snippets.

@techiesatish
Last active April 22, 2018 16:49
Show Gist options
  • Save techiesatish/9339bcc016ed259fc2339f14e8033f96 to your computer and use it in GitHub Desktop.
Save techiesatish/9339bcc016ed259fc2339f14e8033f96 to your computer and use it in GitHub Desktop.
Retrieve contacts in Android. Here we are fetching all the contacts with user number and mobile. You need to add READ_CONTACTS permission Manifest file. Reference: http://www.techiesatish.com/retrieving-contacts-in-listview-in-android/
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lv_Contacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="0dp" />
</android.support.constraint.ConstraintLayout>
public class ContactsActivity extends AppCompatActivity {
ListView lvContacts;
String TAG = ContactsActivity.class.getSimpleName();
ArrayList<String> mContacts;
ArrayAdapter arrayAdapter;
public static final int PERMISSION_REQUEST_CONTACT = 103;
boolean hasPermissionContact;
String userName,MobileNo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
lvContacts = findViewById(R.id.lv_Contacts);
mContacts = new ArrayList<>();
arrayAdapter = new ArrayAdapter<String>(
ContactsActivity.this,
android.R.layout.simple_list_item_1, android.R.id.text1, mContacts);
askForPermission();
lvContacts.setAdapter(arrayAdapter);
}
private void askForPermission() {
hasPermissionContact = (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED);
if (!hasPermissionContact) {
ActivityCompat.requestPermissions(ContactsActivity.this, new String[]{android.Manifest.permission.READ_CONTACTS}, PERMISSION_REQUEST_CONTACT);
}else{
getContacts();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PERMISSION_REQUEST_CONTACT: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(ContactsActivity.this, "Permission Granted.", Toast.LENGTH_SHORT).show();
getContacts();
runOnUiThread(new Runnable() {
@Override
public void run() {
lvContacts.setAdapter(arrayAdapter);
}
});
} else {
Toast.makeText(ContactsActivity.this, "Permission Denied", Toast.LENGTH_LONG).show();
}
}
}
}
public void getContacts() {
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
userName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
MobileNo = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e(TAG, "contacts " + userName + " " + MobileNo);
Collections.sort(mContacts, String.CASE_INSENSITIVE_ORDER); //Sorting of contacts
mContacts.add(userName + " " + "\n" + " " + MobileNo);
}
cursor.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment