Author Archive

Insert and modify Contact in Android

Android 2.0 provides new Contact API, which is defined in  android.provider.ContactsContract.  After some try on contact api I found a better and easy solution for insertion and modification of contact.

Insert contact -

Intent addContactIntent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);

addContactIntent.putExtra(ContactsContract.Intents.Insert.NAME,

"xyz");

addContactIntent.putExtra(ContactsContract.Intents.Insert.EMAIL,

"abc@gmail.com");

addContactIntent.putExtra(ContactsContract.Intents.Insert.PHONE,"012345678");

startActivity(addContactIntent);

ACTION_INSERT will start .EditContact activity.

Edit contacts -
  • If _ID of contact is known -

Intent intent = new Intent(Intent.ACTION_EDIT);

intent.setData(ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, _ID));

startActivity(intent);

  • If _ID of contact is not known -
i) Find out conatct _ID by phone number .
// CONTENT_FILTER_URI allow to search contact by phone number

Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(getPhone()));

// This query will return NAME and ID of conatct, associated with phone //number.

Cursor mcursor = getContentResolver().query(lookupUri,new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID                    },null, null, null);

//Now retrive _ID from query result

long idPhone = 0;

try {
if (mcursor != null) {
if (mcursor.moveToFirst()) {
idPhone = Long.valueOf(mcursor.getString(mcursor .getColumnIndex(PhoneLookup._ID)));
Log.d("", "Contact id::" + idPhone);
}
}
} finally {
mcursor.close();
}

ii) Then Edit contact

if (idPhone > 0) {

Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setData(ContentUris.withAppendedId( ContactsContract.Contacts.CONTENT_URI, idPhone));
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "contact not in list",
Toast.LENGTH_SHORT).show();
}

You can also search _ID by partial name, for this instead of PhoneLookup.CONTENT_FILTER_URI use ContactsContract.Contacts.CONTENT_FILTER_URI
To work with contact, you have to define special permission in AndroidManifest.xml .

<uses-permission android:name="android.permission.READ_CONTACTS" />

<uses-permission android:name="android.permission.WRITE_CONTACTS" />


Nine Patch Images useful for android

Android devices come in different screen sizes and density, often making it very difficult for developer to design an application for all screen sizes. Use of nine patch images can be very helpful for them. Mostly nine patch images are used as background for Buttons, Texts, Frames. Android provide a tool for creating nine patch images. It is available in android-sdk/tools directory named as draw9patch.
What is nine patch?
Nine patch images are simply PNG images with 9 patches as explained in figure -
1,3,7,9 – Not scalable area
2 – Horizontal scalable area
4 – Vertical scale area
6,8 – Text Area
For scaling purpose you have to give guideline for both area(2,4). Otherwise converted nine patch image will not be proper nine patch.
ex -
So basically the intersection of horizontal and vertical guideline’s pixels will replicate. Guidelines for text area is optional.
Nine patch images can not be scale down, so use of mdpi images are baseline for dp scaling(Ignoring ldpi). Here is a link to learn how to draw nine patch images- http://developer.android.com/guide/developing/tools/draw9patch.html

  • TechJini Solutions

  • © Copyright 2009 TechJini Solutions Private Limited. All Rights Reserved
    iDream theme by Templates Next | Powered by WordPress