NOTE: Of late, I have been getting requests for very trivial problems that many of you are facing in your day-to-day work. This blog is not to solve your "project" problems - surely not a "Support" site.
I just love to share my knowledge in my spare time and would appreciate any questions or feedback on the articles and code I have shared. I also do appreciate thought-provoking questions that would lead me to write more articles and share.
But please do not put your day-to-day trivial problems here. Even if you do, you most probably would not get a response here.

Search This Blog


Friday, 25 November 2011

Local WebServer – Apache Tomcat

Pre-requisites: You should have Apache Tomcat installed on your machine, downloadable from  and you should have eclipse 3.6 on your machine
Configure the webserver to be used within eclipse as follows:

1.       Unzip the download into your local directory
2.       Tell Eclipse about Tomcat

First, start Eclipse and go to the Workbench as shown in the previous section. Then, click on Servers tab at bottom. R-click, New, Server, Apache, Tomcat v6.0, navigate to folder, OK. You should now see "Tomcat v6.0 Server at localhost" listed under the Servers tab at the bottom.

3. Run Tomcat.
Click on Servers tab at bottom. R-click on Tomcat v6.0, choose "Start". Open http://localhost/ in a browser: you should see an empty page showing a blank directory listing (but not a 404 error). Eclipse incorrectly fails to copy the welcome pages when it sets up Tomcat, so if you want the friendlier welcome page, go to your-eclipse-workspace\.metadata and search for "ROOT". Copy all of the files from C:\apache-tomcat-6.0.28\webapps\ROOT into the ROOT folder inside your-eclipse-workspace\.metadata\...\ROOT. 
If you fail to copy the ROOT files as mentioned above, http://localhost/ will result in an empty directory listing. It is often mistaken for an error page, but if you look closely you will see that it says "directory listing for /" and is not an error at all. However, if you copy the ROOT files, http://localhost/ will give the nice friendly "Welcome to Tomcat" page.
4.  Make empty project.
a.       File, New, Project, Web, Dynamic Web Project.
b.      Give it a name (e.g., "SampleWebServer").
c.       Accept all other defaults.

5. Make a Welcome.html and place it on the ‘WebContent’ folder of your new Dynamic Web App.
6. Now if you type http://localhost:8080/SampleWebServer/Welcome.html in your browser, you should be able to view the page. 

Basic WebView

On Android we can build what are called Hybrid Apps. i.e. apps that have both HTML pages as well as native activities co-existing. There is a lot of debate on which is the way to develop for mobiles – HTML5 or native? Each of these have their own pros and cons. I will not go into those now. However, to be able to embed HTML pages into your app, one of the ways is through the use of a WebVew component.
Here I will share with you a very simple example of building a WebView App. This is nothing too different from what you see in the google resources pages. However, the intention here is to move from this basic level to a little more advanced level of interacting from the HTML page with Native Apps – integrating through JavaScript. That will be in the next tutorial, in keeping with my principle of explaining one thing at a time.

Also, for this example you will also need to have a webserver that serves you some HTML pages that can be invoked through your WebView. As an addendum to this article, you will find how to create a small web server app in your local environment.

Now, what is a WebView? Google documentation beautifully explains it as
 “A View that displays web pages. This class is the basis upon which you can roll your own web browser or simply display some online content within your Activity. It uses the WebKit rendering engine to display web pages and includes methods to navigate forward and backward through a history, zoom in and out, perform text searches and more.”

Let’s create a WebView which should display your home Page, on loading. In the main class of the project here is the code in the onCreate() method:

  public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "Entering onCreate");
        mWebView = (WebView)findViewById(;
        Log.i(TAG,"Exiting onCreate");

It is simple and straight forward. I have defined a WebView in the layout folder as below (file name webview.xml)

<LinearLayout xmlns:android=""


I get a handle to the WebView in this line: mWebView = (WebView)findViewById(;
And then I load the local URL in this line       mWebView.loadUrl("");

As simple as this. What this does is that it invokes the default android browser if we have links on the welcome page as shown below: (we have gone out of the WebView into a browser app)

If we want to be able to continue opening in the WebView, here is the piece of code that would help. Basically we are writing out own custom browser by extending the WebViewClient:

  private class WebViewSampleClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return true;

Here we are telling to load the passed URL into the WebView by overriding the shoudlOverrideUrlLoading method of the WebViewClient.

After overriding the same, add another piece of code into onCreate() method:
mWebView.setWebViewClient(new WebViewSampleClient());
after mWebView.loadUrl("");

Now when you click on the Second Page link, the new HTML page gets invoked within the WebView as shown below:

From here, if you press a back button, you will log out of the app, since WebView is one activity and the pages are all opened within the WebView. In order to override that behavior and you want to navigate back to the HomePage within the WebView.

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            return true;
        return super.onKeyDown(keyCode, event);

The code is pretty self explanatory. When a back button is clicked and there is something within the WebView that we can go back it, do it within the WebView.  Here is the complete code for download.

Last but not the least, I have also added <uses-permission android:name="android.permission.INTERNET" /> into the AndroidManifest file as this app will use the internet to invoke the http based URL.

To create a Web Server and host HTML pages, please see this tutorial.

NOTE: any webserver running locally can be accessed through http protocol using the IP address or localhost or the actual IP address which you get when you run IP address command at the command prompt. However, within the emulator, the linux kernel takes the localhost as and hence you will see in the code above that this IP address is used.

Friday, 18 November 2011

Android - conversion to dalvik format failed with error 1


I am sure many of you may see this error at sometime or the other when you are developing in Android o Eclipse. Most times, I have found this work for me.

In Eclipse, select the project causing the error- go to the menu project -> properties -> java build path -> libraries and remove all jars including the android jars. 

Then, right click the project, go to android tools -> fix project properties. Then, add back any other external jars you wanted to add to the build path following the same menu project -> properties -> java build path -> libraries. To ensure there are no errors due to missing libraries.

Then, do a 'clean' on menu project and build again. It should work.

My guess is that when an incompatible android jar is used to compile this error is thrown. There may be many other scenarios too that cause thsi error... you can add them below

Monday, 14 November 2011

Localhost Access in Android Emulator

This is a small tit-bit I discovered today:

If you want to access your localhost through the Android Emulator - you need to use the IP Address unlike how we do on the web browsers on our desktops where we access the local host as or 'localhost' itself.

This information is useful if for testing purposes you have locally installed a webserver and put a few HTML pages or if you have locally hosted web services - say REST Services that need to be invoked through a URL like this: http://localhost:8080/rest/hello, then, use the above mentioned IP address and you will be able to access your service or webpage through the emulator.

Or you could simple use your own IP address like - whatever yours is. If you do a "ipconfig" in a windows command prompt, you will know what your IP address is. (one of the ways to find it out). However, there is one disadvantage with this method if your IP is allocated to you dynamically on your network. This will keep changing. In such a case, using will be the equivalent of which will not change for the localhost.

Hope this helps.

Thursday, 23 June 2011

Multiple Ways of Programming for Android

These seem to be more than one way to program for the Android devices.

The first and the most common way is by using the Software Development Kit (SDK) using Java Syntax. This is what I have been talking about in all the earlier posts and tutorials.

The other 3 interesting ways, I believe, are:
2. Native Development Kit using C / C ++. This I am sure many are aware of and would be using to some extent. This is a little more closer to the OS, not running on top of the virtual Machine.
3. RenderScript using C99 - used to write faster graphics code like the Google Books page turn animation etc.
4. Android Scripting Layer using Python etc.

Interesting options, right?

Tuesday, 17 May 2011

Contacts API 2.0 and above | Android Developer Tutorial

Starting from Android 2.0 (API Level 5), the Android platform provides an improved Contacts API for managing and integrating contacts from multiple accounts and from other data sources. To handle overlapping data from multiple sources, the contacts content provider aggregates similar contacts and presents them to users as a single entity. This article describes how to use the new API to manage (insert, update, delete, view) contacts.

The new Contacts API is defined in the android.provider.ContactsContract and related classes. The older API is still supported, although deprecated.

We need to understand the underlying structure of storage to better manipulate the contacts. We have three distinct types of tables – Contacts, Raw Contacts and Data.

All data related to a contact is stored in this generic data table with each row telling what is the data it stores through its MIME type. So we could have a Phone.CONTENT_ITEM_TYPE as the MIME type of the data row, it contains Phone data. Similarly, if we have Email.CONTENT_ITEM_TYPE as the row’s MIME type, then it stores email data. Like this lot of data rows are associated with a single Raw Contact.

Each Raw Contact refers to a specific contact’s data coming from one single source – say, you gmail account or your office Microsoft account.

The Contact is the topmost in the hierarchy which aggregates similar looking data from various sources into one single contact – a very handy feature when you have redundant data coming about the same contact from you various accounts – like a facebook account, orkut account, yahoo account and goggle account.  So the hierarchy looks like this:
So, when we want to insert a new contact, we always insert a Raw Contact. When we want to update an existing contact, we most often deal with the data tables which are accessible through a series of CommonDataKind classes. Because this would be to update particular types of data like phone or email.
Coming to the example:

I create an activity with four buttons to View, Add, Modify and Delete Contacts.

           Button view = (Button)findViewById(;
            Button add = (Button)findViewById(;
            Button modify = (Button)findViewById(;
            Button delete = (Button)findViewById(;
            view.setOnClickListener(new OnClickListener() {
                  public void onClick(View v){
                        Log.i("NativeContentProvider", "Completed Displaying Contact list");

On the click of each of the buttons I invoke their respective methods: like displayContacts(), createContact(), updatecContact() and deleteContact(). We will now see each of these methods.

displayContacts() is pretty straightforward. Access to each of the tables mentioned above is through a content URI. I use the topmost level ‘Contacts’ URI to iterate through all the existing contacts and display their names and phone numbers (Toast them).

We know Contacts is a ContentProvider and hence we need to query through a ContentResolver which returns all the data of the contacts.

   private void displayContacts() {
      ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                  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) {
                     Cursor pCur = cr.query(
                               ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                               new String[]{id}, null);
                     while (pCur.moveToNext()) {
                         String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                         Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();

I will try to briefly explain the above method.  Line 1 gets a handle to the ContentResolver. Line 2 queries the Contacts URI (ContactsContract.Contacts.CONTENT_URI ) without any mention of the specific columns or “where” clause of an SQL query. Notice all the 4 parameters are null. This means that we are not making any conditional query into the contacts table and hence all data is returned into the cursor.

Next, I check that the cursor is not empty and iterate through the cursor data. I retrieve the _ID and DISPLAY_NAME from the Contacts and then, I check for the flag if the contact has a phone number. This information is available in contacts table itself. But the Phone number details are in the data tables. Hence after checking for the flag, I query the CommonDataKings.Phone.CONTENT_URI for the phone data of that specific ID. From this new cursor named pCur, I retrieve the Phone Number. If there are multiple phone number for one contact, all of them will be retrieved and toasted one after another.
Now, let us see how to create or insert a new contact.

In the createContact() method which is called when you click on the “Add Contact” button, I first query to see if the hardcoded name “Sample Name” already exists. If so, I toast a message stating the same. If not, then I get into actually inserting the name along with a phone number. The first part of the check you can view in the complete source code available for download. Only the second part of inserting a contact is explained here. For this, we need to use a ContentResolver always.

A ContentResolverprovides an applyBatch(…) method which takes an array of ContentProviderOperation classes as a parameter. All the data built into the ContentProviderOperations are committed or inserted into the ContentProvider that we are working on. IN this case, the ContentProvider we are working on are Contacts and the authority associated with the same is ContactsContract.AUTHORITY

Here is the code for the same:

        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "")
                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, "")
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME)

        try {
                  cr.applyBatch(ContactsContract.AUTHORITY, ops);

In the first element in the ops array, I am setting the details of the account to which I want to add the contact. In this case the gmail account type is and the account name is “”. The latter has to be unique and hence it is recommended to use the internet URLs of the source of data.

In the second element, I am adding the name of the contact and in the third the phone data. You notice that I use .withValueBackReference(..) as we still have not created the Raw contact and hence we do not have the Id. The first row creates the id and hands over the id to the next rows of data. 
This array ops is passed into the ContentResolver and thus the data is inserted.

For updating the phone number of an existing contact, I again use the ContentResolver with the ContentProviderOperation array. However, now, I pass the where clause and the parameters of the where clause – specifically indicating that only the phone number of the “Sample Name” contact has to be updated.
                    .withSelection(where, params)
                    .withValue(ContactsContract.CommonDataKinds.Phone.DATA, phone)

Notice the .withSelection(where, params). The where and params look like this:

        String where = ContactsContract.Data.DISPLAY_NAME + " = ? AND " +
                        ContactsContract.Data.MIMETYPE + " = ? AND " +
                        String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE) + " = ? ";
String[] params = new String[] {name,

Delete too is done in a very similar manner with no values but only the selection criteria is provided for which contact to be deleted.

      ContentResolver cr = getContentResolver();
      String where = ContactsContract.Data.DISPLAY_NAME + " = ? ";
      String[] params = new String[] {name};
        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
              .withSelection(where, params)
        try {
                  cr.applyBatch(ContactsContract.AUTHORITY, ops);
            } catch (RemoteException e) {

I really scouted the internet a lot to find a comprehensive tutorial on the new Contacts Content Provider API but could not find anything that easily. Hope this helps all who of you who are looking for the same.

NOTE: Many have been asking me questions on why the contacts that have just been added through this app are not visible in the phone contacts. All your contacts on phone are linked to your google account on the phone. However, the contacts from the above code have got added to psuedo google account and hence will not be visible directly. You search for the just added contact and you will be able to find it.

Hope this helps.