Build a Simple Contact Application With Calling Feature

This post will help you to create your own Simple contact application which fetches all the contacts from the database and display it in a ListView. This actually accesses the contact database of the phone by a cursor adapter. Then it stores The name and the phone number associated in a listview using base adapter class. The basic function of this app is to call that number when clicked on a specific contact.

Demo:



  (a) The MainActivity, showing the List Of Contacts.              (b) When tapped, a call is placed by Intent.


The Contact fetching code...
public void addi()
    {
    	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
          s.add(name);
          path.add(phoneNumber);
          
         }
        phones.close();
    }

Adding the Contact name and the phone number in the ListView
private void getDataInList() 
    {
        for(int i=0;i<s.size();i++) 
        {
                // Create a new object for each list item
                ListData ld = new ListData();
                ld.setTitle(s.get(i));
                ld.setPath(path.get(i));
                // Add this object into the ArrayList myList
                myList.add(ld);
        }
        
    }
Adding intent to onItemClick function:
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		Intent i=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+path.get(arg2)));
		startActivity(i);
	}

Alternatively you can download the whole project here.

Found any bugs, feel free to report them, we will be glad to fix them! :)

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Bit Stuffing Code Implementation in Java

Hackerrank Modified Kaprekar Numbers Solution