This video shows the steps to develop an Android App to read or access the contact details and information of the numbers stored in the phone’s database.
This App considers both the use case of accessing the contact details either with Number or Name.
If the Number is entered or provided then the respective Name is returned and printed in the edit text for display.
Similarly, if the Name is entered or provided then the respective Phone number is retrieved and displayed on the respective edit Text.
This concept can be used in a larger App, which needs the access to the address book stored in the phone to read and write the contact details.
I hope this video is useful to you. For any suggestions, queries or appreciation, please email us at: programmerworld1990@gmail.com
Source code:
Java code
package com.example.mycontactaccessapp; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import android.Manifest; import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.view.View; import android.widget.EditText; public class MainActivity extends AppCompatActivity { private EditText editText1; private EditText editText2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CONTACTS, Manifest.permission.READ_CONTACTS}, PackageManager.PERMISSION_GRANTED); editText1 = findViewById(R.id.editText); editText2 = findViewById(R.id.editText2); } public void getNameButton(View view){ try { Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(editText2.getText().toString())); Cursor cursor = getContentResolver().query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null); String stringContactName = "INVALID"; if (cursor != null){ if (cursor.moveToFirst()){ stringContactName = cursor.getString(0); } } editText1.setText(stringContactName); } catch (Exception e){ e.printStackTrace(); } } public void getNumberButton(View view){ try { Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE}, "DISPLAY_NAME = '" + editText1.getText().toString() + "'", null, null); cursor.moveToFirst(); editText2.setText(cursor.getString(0)); } catch (Exception e){ e.printStackTrace(); editText2.setText("NA"); } } }
Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mycontactaccessapp">
uses-permission android:name="android.permission.WRITE_CONTACTS"/>
uses-permission android:name="android.permission.READ_CONTACTS"/>
application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
activity android:name=".MainActivity">
intent-filter>
action android:name="android.intent.action.MAIN"
category android:name="android.intent.category.LAUNCHER"
/intent-filter;
/activity;
/application
</manifest>
Layout XML file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">
 lt;EditText
android:id="@+id/editText"
android:layout_width="152dp"
android:layout_height="46dp"
android:layout_marginStart="29dp"
android:layout_marginTop="102dp"
android:ems="10"
android:hint="@string/contact_name"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:autofillHints="" />
<EditText
android:id="@+id/editText2"
android:layout_width="146dp"
android:layout_height="38dp"
android:layout_marginStart="54dp"
android:layout_marginTop="103dp"
android:ems="10"
android:hint="@string/contact_number"
android:inputType="textPersonName"
app:layout_constraintStart_toEndOf="@+id/editText"
app:layout_constraintTop_toTopOf="parent"
android:autofillHints="" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="52dp"
android:layout_marginTop="60dp"
android:onClick="getNameButton"
android:text="@string/get_name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="106dp"
android:layout_marginTop="64dp"
android:onClick="getNumberButton"
android:text="@string/get_number"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/editText2" />
</androidx.constraintlayout.widget.ConstraintLayout>
Estimados señores promgramer world: me pareció muy interesante su video sobre como acceder a los contactos, de hecho copié el código para crear la aplicación en android studio pero no me corre como debería, cuando le doy el nombre no me encuentra el número del contacto, en ocasiones lo encuentra pero en raras ocasiones. en cambio fluye mejor cuando le introduzco el numero.
Como programador le sugeriría que si puede revisarlo y probarlo en su celular sería genial. y si le funciona le agradezco me regale el programa corregido.
un abrazo.
In this code, it simply uses ContentResolver to query the contact name or number from the content URI. So, if it works for number it should work for name as well. Also, the App’s code, posted here, is tested on both emulator (as shown in the video) and physical phone. If in some cases it is not working for names then it could be because of name mismatch like special characters, etc. If you have debugged the code and are observing any error or exception then post it here. We will be happy to check it.
Cheers
Programmer World
–