076 Java Android Program to Demonstrate Finding Contact Details for a Contact name
Posted by Superadmin on February 27 2017 12:44:58

Java Android Program to Demonstrate Finding Contact Details for a Contact name

Here is source code of the Program to Demonstrate Finding contact details for a contact name. The program is
successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

 

The following code shows how to use the contact-detail column names available in the CommonDataKinds subclasses to extract the display name and mobile phone number from the Data table for a particular contact.

MainActivity.java

package com.example.contactdetailcontactname;
 
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button detail = (Button) findViewById(R.id.butDetails);
        detail.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                findDetail();
            }
        });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
    public void findDetail() {
        ContentResolver cr = getContentResolver();
        String[] result = null;
        // Find a contact using a partial name match
        String searchName = "specify serach name here";
        Uri lookupUri = Uri.withAppendedPath(
                ContactsContract.Contacts.CONTENT_FILTER_URI, searchName);
        // Create a projection of the required column names.
        String[] projection = new String[] { ContactsContract.Contacts._ID };
        // Get a Cursor that will return the ID(s) of the matched name.
        Cursor idCursor = cr.query(lookupUri, projection, null, null, null);
        // Extract the first matching ID if it exists.
        String id = null;
        if (idCursor.moveToFirst()) {
            int idIdx = idCursor
                    .getColumnIndexOrThrow(ContactsContract.Contacts._ID);
            id = idCursor.getString(idIdx);
        }
        // Close that Cursor.
        idCursor.close();
        // Create a new Cursor searching for the data associated with the
        // returned Contact ID.
        if (id != null) {
            // Return all the PHONE data for the contact.
            String where = ContactsContract.Data.CONTACT_ID + " = " + id
                    + "AND" + ContactsContract.Data.MIMETYPE + " = ‘"
                    + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
                    + "’";
            projection = new String[] { ContactsContract.Data.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER };
            Cursor dataCursor = getContentResolver().query(
                    ContactsContract.Data.CONTENT_URI, projection, where, null,
                    null);
            // Get the indexes of the required columns.
            int nameIdx = dataCursor
                    .getColumnIndexOrThrow(ContactsContract.Data.DISPLAY_NAME);
            int phoneIdx = dataCursor
                    .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
            result = new String[dataCursor.getCount()];
            while (dataCursor.moveToNext()) {
                // Extract the name.
                String name = dataCursor.getString(nameIdx);
                // Extract the phone number.
                String number = dataCursor.getString(phoneIdx);
                result[dataCursor.getPosition()] = name + "(" + number + ")";
                Toast.makeText(this, name + "(" + number + ")",
                        Toast.LENGTH_SHORT).show();
 
            }
            dataCursor.close();
        }
    }
}

Activity_main.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <Button
        android:id="@+id/butDetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="100dp"
        android:text="Find Details" />
 
</RelativeLayout>

AndoridManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.contactdetailcontactname"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
 
    <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="com.example.contactdetailcontactname.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>