Android Tutorial , Programming Tutorial, Php Tutorial, Learn Android, HTML Tutorial, Coding , Java Tutorial, GTU Programs, Learning Programming

Showing posts with label Android Reading Phone Contact Example. Show all posts
Showing posts with label Android Reading Phone Contact Example. Show all posts

Friday, 7 October 2016

Read all Contact's Phone Numbers in Android

This example is show that how to read phone contacts display them in ListView. Let's know try this example.

File Name : MainActivity,java



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.example.readcontacts;

import java.util.ArrayList;
import java.util.HashSet;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
 private ListView mListView;
 private ProgressDialog pDialog;
 private Handler updateBarHandler;
 StringBuffer output;
 ArrayList<String> contactList;
 Cursor cursor;
 String contactName,phNumber;
 int counter;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  pDialog = new ProgressDialog(this);
  pDialog.setMessage("Reading contacts...");
  pDialog.setCancelable(false);
  pDialog.show();

  mListView = (ListView) findViewById(R.id.list);
  updateBarHandler =new Handler();
  
  // Since reading contacts takes more time, let's run it on a separate thread.
  new Thread(new Runnable() {

   @Override
   public void run() {
    getContacts();
   }
  }).start();

  // Set onclicklistener to the list item.
  mListView.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View view,
     int position, long id) {
    //TODO Do whatever you want with the list data
    Toast.makeText(getApplicationContext(), "item clicked : \n"+contactList.get(position), Toast.LENGTH_SHORT).show();
   }
  });
 }

 public void getContacts() {

  output = new StringBuffer();
  contactList = new ArrayList<String>();
  

  
        Cursor c = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                null, null,"upper("+Phone.DISPLAY_NAME+ ") ASC");
        while (c.moveToNext()) {

            contactName = c
                    .getString(c
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            
            phNumber = c
                    .getString(c
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            contactList.add(contactName + "\n" + phNumber);
            
        }
        c.close();     
   

   // ListView has to be updated using a ui thread
   runOnUiThread(new Runnable() {

    @Override
    public void run() {
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, R.id.text1, contactList);
     mListView.setAdapter(adapter);   
    }
   });

   // Dismiss the progressbar after 500 millisecondds
   updateBarHandler.postDelayed(new Runnable() {

    @Override
    public void run() {
     pDialog.cancel();    
    }
   }, 500);
  }
 
 }


File Name : activity_main.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<RelativeLayout 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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#95a5ff"
        android:dividerHeight="1dp"
        />
</RelativeLayout>



File Name : list_item.xml



1
2
3
4
5
6
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:textColor="@android:color/black" />


Like us on Facebook

Site Visitor

Powered by Blogger.