Wednesday, 17 February 2016

Android Sample code to Get Contact list in ListView and Make Call onClick of Item



MainActivity.java

package com.example.mhosamani.phonecall;
import android.content.ClipData;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    TextView
textDetail;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        readContacts();
    }

   
public void readContacts()
    {
        ArrayList<String> phoneList=
new ArrayList<String>();
        StringBuffer sb =
new StringBuffer();
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        String phone =
null; String emailContact = null; String emailType = null; String image_uri = ""; Bitmap bitmap = null;
       
if (cur.getCount() > 0)
        {
           
while (cur.moveToNext())
            {
                sb.delete(
0, sb.length());
                String id = cur.getString(cur .getColumnIndex(ContactsContract.Contacts.
_ID));
                String name = cur .getString(cur .getColumnIndex(ContactsContract.Contacts.
DISPLAY_NAME));
               
if (Integer .parseInt(cur.getString(cur .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
                {
                    sb.append(name+
":\n");
                    Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.
CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
                   
int i=0;
                   
while (pCur.moveToNext())
                    {
                        phone = pCur .getString(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.
NUMBER));
                        
if(i==0)
                        sb.append(phone);
                        i++;
                        Log.d(
"Item",String.valueOf(sb));
                    }
                    phoneList.add(String.valueOf(sb));
                    pCur.close();

                }
            }

        }

       
final ListView listview = (ListView) findViewById(R.id.listview);
       
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, phoneList);
                listview.setAdapter(adapter);
                listview.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
                   
@Override
                   
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String ItemSelected=(String)
listview.getItemAtPosition(position);
                        String ph[]=ItemSelected.split(
"\n");
                        Log.d(
"ph",ph[1]);
                        Intent phoneIntent =
new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + ph[1]));
                       
/*Intent phoneIntent = new Intent(Intent.ACTION_CALL,Uri.parse("+91 94 48 364384"));*/
                        //Intent in=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+ph[1]));
                       
try{
                            startActivity(phoneIntent);
                        }
                       
catch (android.content.ActivityNotFoundException ex){
                            Toast.makeText(getApplicationContext(),
"yourActivity is not founded", Toast.LENGTH_SHORT).show();
                        }


                    }
                });

    }
}



AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   
package="com.example.mhosamani.phonecall">

    <
application
       
android:allowBackup="true"
       
android:icon="@mipmap/ic_launcher"
       
android:label="@string/app_name"
       
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>
    <
uses-permission android:name="android.permission.READ_CONTACTS" />
    <
uses-permission android:name="android.permission.CALL_PHONE" />
    <
uses-permission android:name="android.permission.READ_PHONE_STATE" />

</
manifest>


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.mhosamani.phonecall.MainActivity">

    <
TextView
       
android:id="@+id/title"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_alignParentLeft="true"
       
android:layout_alignParentTop="true"
       
android:layout_margin="10dp"
       
android:textSize="25dp"
       
android:text="Contact Details" />

    <
ListView xmlns:android="http://schemas.android.com/apk/res/android"
       
android:id="@+id/listview"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_below="@+id/title"/>


</
RelativeLayout>