Search Tutorials

Loading...

Monday, 11 November 2013

Display all Mobile Contacts in Android

In this application, we will learn how to read mobile contacts and display all the contacts on text view. We used a text view in scroll view to display all the contacts with their names. Create a new project, open the XML file and drop text view in scroll view layout. The code of android XML file is given below:

Display all Mobile Contacts in Android
Display all Mobile Contacts in Android

<LinearLayout 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:background="#abc">
<ScrollView
  android:id="@+id/scrollView1"
  android:layout_width="wrap_content"
  android:layout_height="match_parent" >
<TextView
  android:id="@+id/text1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="25sp"
  android:text="" />
</ScrollView>
</LinearLayout>

Now open your Java file and paste the code below. To get all your phone contacts, use a cursor object because it can keep a variable of any datatype. A cursor is mostly used in databases like SQLite in a select query or during the fetching of data from a table which has different types of variables, etc. Use moveToNext() method in the while loop to get row one by one from the cursor, use getString() method to get the data one by one from a row and use getColumnIndex() method to get column index using index value. The code of the android Java file is given below with explanation:

package sel.con_name; //your package name

import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.TextView;
import android.app.Activity;
import android.database.Cursor;

public class MainActivity extends Activity {
  TextView tv;
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  tv=(TextView)findViewById(R.id.text1);
  //use cursor to keep any type of data
  //take all mobile contacts database in cursor
  Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.
Phone.CONTENT_URI, null,null,null, null);
  while (phones.moveToNext())
  {
    //get name and number from cursor using column index
    String name=phones.getString(phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
    String phoneNumber = phones.getString(phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
    //display on text view
    tv.append(name+"->"+phoneNumber+"\n");
   }
  phones.close();
  }
}

Now open your AndroidManifest.xml file and give permission to read contacts from mobile. The code of the AndroidManifest.xml file is given below:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="sel.con_name"
  android:versionCode="1"
  android:versionName="1.0" >
<uses-sdk
  android:minSdkVersion="10"
  android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application
  android:allowBackup="true"
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/AppTheme" >
<activity
  android:name="sel.con_name.MainActivity"
  android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Now run your project and test the application. If you have any doubts please comment. Share and help all developers.

Related Tutorials:-

Get battery level using Broadcast Receiver

Display Time using Broadcast Receiver

Warning: No DNS servers found in Android Eclipse

Extract APK File into source code (Java code and XML code)

Run two or more instances of emulator at a time

11 comments:

  1. Good brother!!! thanks for sharing....

    ReplyDelete
  2. Best friend! Thank for sharing

    ReplyDelete
  3. I am receiving double contacts using this source code ,,, I am using android 4.0.3 android version with synchronized my contacts to google servers also

    ReplyDelete
    Replies
    1. Check contacts in mobile because you may have double contacts or check in your code because it will display all contacts from your contact list only one time.

      Delete
  4. How could i encrypt and/or decrypt the contacts using the AES algorithm and still how could i hide and reveal them. thanks

    ReplyDelete
  5. is there any way to get all the contacts to our email id ?

    ReplyDelete
    Replies
    1. Yes save all contacts in a string and mail.

      Delete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete

Back to Top