MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.bhaumik.readcontectdemo.MainActivity">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Text Name"
android:layout_marginTop="20dp"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Hello"
android:layout_marginTop="20dp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000"
android:layout_marginTop="10dp"/>
</LinearLayout>
ContactData.java
package com.example.bhaumik.readcontectdemo;
/**
* Created by Bhaumik on 9/6/2017.
*/
public class ContactData {
String name,number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
MainActivity.java
package com.example.bhaumik.readcontectdemo;
import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView listView;
ContactData contactData;
List<ContactData> list = new ArrayList<>();
ContactAdapter adapter;
int counter;
private static final int REQUEST_CODE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_CODE);
}else{
getContacts();
}
}
private void getContacts() {
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,null,null,ContactsContract.CommonDataKinds.Phone.NUMBER);
if(cursor.getCount()>0){
counter =0;
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME ));
String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactData = new ContactData();
contactData.setName(name);
contactData.setNumber(phone);
list.add(contactData);
}
adapter = new ContactAdapter(MainActivity.this,R.layout.item_list,list);
listView.setAdapter(adapter);
}
cursor.close();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission is granted
getContacts();
} else {
Toast.makeText(this, "Permission Cancel.", Toast.LENGTH_SHORT).show();
}
}
}
}
ContactAdapter.java
package com.example.bhaumik.readcontectdemo;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.amulyakhare.textdrawable.TextDrawable;
import com.amulyakhare.textdrawable.util.ColorGenerator;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Bhaumik on 9/6/2017.
*/
public class ContactAdapter extends ArrayAdapter<ContactData>{
Context context;
List<ContactData> list = new ArrayList<>();
public ContactAdapter(@NonNull Context context, @LayoutRes int resource,List<ContactData> list) {
super(context, resource,list);
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Nullable
@Override
public ContactData getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null){
viewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_list,parent,false);
viewHolder.name = convertView.findViewById(R.id.text_name);
viewHolder.number = convertView.findViewById(R.id.text_view);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.name.setText(list.get(position).getName());
viewHolder.number.setText(list.get(position).getNumber());
return convertView;
}
public class ViewHolder{
TextView name,number;
}
}