Back to index
Trivial but not documented elsewhere.

I was hacking on the way2droid android app - and wanted to add the option to display sim contacts and allow the user to choose from it. This thing seems to be very trivial, but there is no straight forward documentation for it all over the web. I have to google a lot of links and settled on this below way. This piece of code uses a ListActivity to display the list of all sim contacts.

First we should have a Contact class, which can store the name and number of the sim contacts.

The sim contacts can be queried using the uri - "content://icc/adn/". I have created a ContactActivity class which extends the ListActivity and retreived the simcontacts into a ArrayList of Contact type. We can provide the data to the ListActivity through adapters. I have used a ArrayAdapter in this example. The ListActivity has some default layout in which it will display the contact items. If you want you can even supply your own layout. I am not going to explain that part. Every Activity should have a onCreate method, where you will initialise stuff for that. Below is our ContactActivity class.

In the onCreate method, I have first set the custom layout I created for this activity. Then I retreive all the sim contacts through retreiveSimcontacts. Then we assign a TextWatcher for that search field, so that we can filter the results through the contact list as and when the user types in. Assume I have added the searchbox in the layout and it's id is search_box. We have to add a TextWatcher class to this which will monitor the text being typed in the box and call the appropriate methods. The TextWatcher should implement these three methods

  • afterTextChanged
  • beforeTextChanged
  • onTextChanged

Here we are using only the onTextChanged method.

Next thing worth explaining is the onListItemClick method. This method is where we extract the clicked contact details and send it back to the caller. I have set the Result of ContactActivity there and end the activity so that it can return to from where it is called (i.e) the parent activity.

The retreiveSimContacts method is pretty much self explanatory. I am creating a ArrayList and appending all the sim contracts as we retreive them from the above mentioned URI.

Note : This is just the Activity used to retreive and select the contacts. You should call this activity from some where, so that it \ will return a 'name:number' string to you.

To have a look at the complete code - original repo and git mirror.

Published at : Saturday Mar,10 2012 at 18:33 | Tags : hacking, android, | Comments
blog comments powered by Disqus Browse Archives
I am..
Azhagu Selvan SP
Atheist, FOSS enthusiast,
Pythonist, Student
Creative Commons License