I started to learn Mobile App Development with Android Studios. I want to create a simple app(for testing) where I display all cellphone contacts in a ListView.
I have the main activity and its xml file where I have added a ListView element, I want to display all contacts there.
This is the code of the main activity class:
package com.example.contactlist;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
this is the code of the main activity XML:
<?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">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="66dp"
android:background="@color/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="Contact Lists" />
<TextView
android:id="@+id/subtitulo_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact Lists"
android:textColor="@android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.087"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.119" />
<ListView
android:id="@+id/listado_contactos"
android:layout_width="346dp"
android:layout_height="644dp"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="21dp"
android:layout_marginEnd="23dp"
android:layout_marginRight="23dp"
android:layout_marginBottom="29dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/subtitulo_container"
tools:listitem="@android:layout/simple_list_item_multiple_choice" />
</androidx.constraintlayout.widget.ConstraintLayout>
and this is the code of the AndroidManifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.contactlist">
<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>
What I want to know is how can I get all contacts data from the cellphone and display them in my ListView?
I've developed a utility class to retrieve device contacts. It's available on GitHub:
Since handling all situations that might happen in retrieving contacts is a bit time consuming, I suggest you get this file and add it to your project. It's written in kotlin
, but if you are using java
, also it's possible to get the list of contacts like the following:
List<ContactData> contacts = ContactUtilsKt.retrieveAllContacts(context);
// or to retrieve all contacts matching specific search pattern:
List<ContactData> contacts = ContactUtilsKt.retrieveAllContacts(context, "John");