Monday, October 25, 2010

Adding Contacts Using the Android SDK

There are a number of good blogs about building Android apps, but Christophe Coenraets has one of the better ones out there right now. He builds a contact application in 6 projects that start off small and build in complexity. The final sample uses a SQLite database to feed the application. I will not rehash what he has done, but one thing I added was the ability to add the contact to the actual Android contacts applet using the code snippet below. As you can see this is straightforward and requires little code to implement.

Intent addContactIntent = new Intent();
addContactIntent.setAction(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT);
addContactIntent.addCategory(Intent.CATEGORY_DEFAULT);
addContactIntent.setData(Uri.fromParts("tel", officeNumber, null));
addContactIntent.putExtra(ContactsContract.Intents.Insert.NAME, employeeNameText.getText());
addContactIntent.putExtra(ContactsContract.Intents.Insert.EMAIL, emailAddress);
this.startActivity(addContactIntent);



Another slight modification from the original work was to use a different icon for the launcher. There are three screen densities for the Android: Low, Medium and High. Low density (ldpi) uses a 36x36 px PNG file, Medium density (mdpi) uses 48x48 and High density (hdpi) uses 72x72. These three densities each have a folder in the project under the res folder. Simply dropping a png file named icon.png into each of these three folders will change the icon of your application. There is a table at developer.android.com as a reference.

No comments:

Post a Comment