Adding request header to cordova webview

To add headers while making request to URL through Cordova WebView following steps has to be followed:

  1. Create a HashMap and add the header parameters to this HashMap
  2. Pass the HashMap to WebView’s loadUrl function along with the URL
  3. In shouldOverrideUrlLoading method of CordovaWebViewClient or WebViewClient add the same HashMap to WebView’s loadUrl function along with the URL

Creating a HashMap:

Map<String, String> map = new HashMap<String, String>();

map.put(“header_name”, “value”);

Passing the header value while calling loadUrl:

loadUrl(url, map);

Passing the same headers in shouldOverrideUrlLoading:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url, map);
return true;

Note: The HashMap which contains the headers should be passed while calling loadUrl of WebView as well as shouldOverrideUrlLoading of WebViewClient.

Not able to generate apk

If you are not able to build your project even after downloading and installing ADT properly then you must have missed Downloading Build tools from Android SDK Manager.

Open your Android SDK Manager and look for Android SDK Build-tools 



If Android SDK Buil-tools is not installed then this is what blocking you from generating apk. Install it and it and everything should work fine.  Make sure that there is no compilation issues in your resource folder.

Possible errors while running test cases

While writing functional or unit test cases through Robotium or  InstrumentationTestCase if you get

TestCase class com.example.test.WelcomeActivityText is missing a public constructor with no parameters or a single String parameter

at run time then this must be because of your test project dint include a default constructor. That is

import com.example.WelcomeActivity;
import android.test.ActivityInstrumentationTestCase2;

public class WelcomeActivityText extends ActivityInstrumentationTestCase2<WelcomeActivity> {

    private Solo mSolo;
    public WelcomeActivityText() {
    protected void setUp() throws Exception {
        mSolo = new Solo(getInstrumentation());
    public void testWelcomeActivity(){
        assertEquals(WelcomeActivity.class, getActivity().getClass());
        mSolo.assertCurrentActivity("WelcomeActivity is not shown", WelcomeActivity.class);


If you create a default constructor like the above code you can get ride of this error.

Invalid Package: ” could not be found or has no tests”

If you get this error means then please do check you have started your test case function with test as prefix (testWelcomeActivity).

Problem While triggering Notification

To create a notification refer this link.

Mostly everything should work perfectly. Suppose if you get the bellow error

Suppressing notification from package com.example.notify by user request  then it means that notification is blocked by the Android Framework since the Show notifications check box in Application Manager is unchecked. By default this check box will be checked but there are possibilities that this may get unchecked (I have seen its getting unchecked in one Samsung Nexus S device). Just checking the check box will solve your problem. Show Notifications is available inside Setting->Application Manager ->Downloads->Your Application

Application Manager App

Application Manager App

Restructuring DatePickerDialog

If you want to restrict your user not to enter date lesser then the current date from DatePickerDialog then you can use the below code as such :

try {
            DateTime dt = new DateTime(desiredDate, DateTimeZone.UTC);

            DatePickerDialog selectDatePicker = new DatePickerDialog(this, mDateSelectedListener, dt.getYear(),
                    dt.getMonthOfYear() - 1, dt.getDayOfMonth());
            java.lang.reflect.Field mDatePickerField = selectDatePicker.getClass().getDeclaredField("mDatePicker");
            DatePicker mDatePicker = (DatePicker) mDatePickerField.get(selectDatePicker);

            mDatePicker.init(dt.getYear(), dt.getMonthOfYear() - 1, dt.getDayOfMonth(),
                    new OnDateChangedListener() {

                        public void onDateChanged(DatePicker view, int year, int monthOfYear,
                                int dayOfMonth) {

                            if (isDateAfter(view)) {
                                Calendar mCalendar = Calendar.getInstance();
                                        mCalendar.get(Calendar.DAY_OF_MONTH), this);

                        private boolean isDateAfter(DatePicker tempView) {
                            Calendar mCalendar = Calendar.getInstance();
                            Calendar tempCalendar = Calendar.getInstance();
                            tempCalendar.set(tempView.getYear(), tempView.getMonth(),
                                    tempView.getDayOfMonth(), 0, 0, 0);
                            if (tempCalendar.before(mCalendar))
                                return true;
                                return false;
        } catch (NoSuchFieldException e) {
        } catch (IllegalArgumentException e) {
        } catch (IllegalAccessException e) {

Motorola Droid2 problem with listview default behaviour

Have you tested your application on all the Android devices ? If you dint then you might be in risk. Some devices in android uses device custom theme to render all its application. Let me explain this with Motorola Droid2 device. I am trying to add a simple ListView in my app. Here is the code which i used to generate it.


<RelativeLayout xmlns:android=""
 android:layout_height="match_parent" >


public class DroidFixes extends Activity {
 CustomAlertDialog dialog;
 ListView list;
 public void onCreate(Bundle savedInstanceState) {
 String s[] ={"1","2","3"};
 list = (ListView)findViewById(;
 ArrayAdapter<String> arr = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line,s);

This results in :


If you closely observer you would’ve found a grey background in the lower half of the ListView. This will not be seen in other devices are even in the emulator. This is the custom behavior of ListView which is implemented in Motorola Droid2. This happen because in the above xml layout ListView’s height is specified as wrap_content.


To avoid this there are many solution

  1. Change the ListView height attribute to wrap_content. Since the list content is small it couldn’t occupy the entire screen so the remaining space is filled with the device theam, setting the listview height attribute to wrap_content will wrap perfectly to the list content without occupying any extra space.
  2. If you insist to go with match_parent for listview height attribute then you can set android:overScrollFooter=”@null” this will remove the drawable in the ListView footer content.
  3. If this dint solve your problem then if you have any inflated custom row for this ListView set a background color to that view.


Applying Solution 1

Since i dint use any custom views or layout for this ListView row i am just changing the android:layout_height to “wrap_content” to attain this output.

Applying Solution 2

Things To Note:

Compare the solution 2 with 1. Since ListView’s height is left to match_parent the bottom most divider line is visible if you set it as wrap_content then you wont get the bottom most divider.

Hope this solves your problem 🙂

Android Development Tools

Development in Android is made very easy by providing the ADT (Android Developer Tools) plug-in for Eclipse. This ADT plug-in provides GUI access for the command line tools.

Lets see about :

  • Logcat
  • Debugger
  • Drag and drop options

Debugger :

In Android Debug comes as an perspective. Open your .Java file from your project in Eclipse IDE and double click on the line where you want to set a break point.

The line int debug=0 is set as the debug line. This means that the program execution will be stopped when this particular line reaches.  Now press F11 key to start debugging. An Conform perspective switch window will appear requesting you to switch to debug perspective. Hit yes to switch to debug perspective. Press F5 to step into the debugging point.

Debugging point

At this point the debug value is 0 which is denoted in the variables window. Press F6 to go to the next line. Now the debug value changes to 1 because of the increment.

Press F8 to completely Run the code.

Drag and Drop :

In Android front end design can be simply done by dragging and dropping. Instead of typing for each and every Views in a design this helps to finish your work soon. To make use of the drag and drop option first right click on the .xml file in your project. Click on the open with -> Android Layout Editor and select Graphical Layout in the bottom of the screen.

Just drag and drop the views from the left widget and place it over the right side screen. There is an excellent video in the developers site regarding Development Tools in android this will help you to understand the various features which will help you save time in designing.


Complete Steps to install Android in Ubuntu

Just go through Installation and Hello world pdf for step by step  procedure for setting up development environment in your Ubuntu operating system.  Since Eclipse is an common IDE most of the steps remains same for other operating systems.

Recommended Software:

  • Java 6
  • 32-bit Eclipse IDE (classic or Java)
  • ADT
  • Android SDK

All the software mentioned above are open source software (*you have open-java) 🙂 .

Go through the pdf for Installation steps.

Using android:layout_weight attribute

As discussed earlier i have found an temporary solution which can be used for your design to occupy your total screen space in a proportionate way. android:layout_weight attribute can be used to allocate the size of the View according to the screen size.  If we use android:layout_width=”100dp” or android:layout_width=”wrap_content” only the specified space is occupied.


Consider that you need to place 2 buttons at the end of the screen which occupies the entire bottom part. If you use layout_width=150dp respectively for both the buttons it will look like this in 3.7″ screen and it gives an impression that the screen is occupied wisely.

Run the same code in 7″ screen even though you have used dp for setting the size of the buttons, it wont full fill your current desire. The output will look like


Consider the bottom most part as an individual layout and add the layout_weight attribute to scale up the View elements according to the screen size.

To use layout_weight the layout_width or height of the layout must be using fill_parent

In the bellow example android:layout_width=”fill_parent” which denotes that weight is applied only to the width of the particular view not to it height. weightSum attribute take up the entire space of the layout which has to be split in an integer value, in our case we have only 2 buttons which occupies equal width, so weightSum is set to 2 and the layout_weight value of the views is specified as 1 for both the views. Each view occupies 1/2 (layout_weight/weightSum) of the space.

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout android:layout_alignParentBottom=”true”
android:baselineAligned=”true” android:weightSum=”2″


Now you are able to get the desired design across multiple screens with a single xml file. layout_weight will be a useful attribute if you use it wisely.

Working with Google Maps in Android

Google Provides an API to work with google maps. This API is available for update through your android sdk manager. To access the google map in your application you have to register and obtain a Map API key. Even for testing google map in emulator you need this key.


Download the Google API.

Select the Google APIs by Google Inc. option and click on Install pac button.


Create a Emulator for running this google map. It doesn’t run in your default Emulators, to run a Map application you need a emulator which supports google map. To create a emulator open AVD Manager.

  1. Type a name for your emulator
  2. Select the target as Google API
  3. Specify the SD Card size.
  4. Click on Create AVD button.

See bellow the emulator with name which you specified is ready.


Now you have to generate a MD5 finger print for registering yourself with google for using its google map data.

To generate MD5 you have to use keytool, this tool takes keystore as argument and generates MD5 finger print. This MD5 finger print is necessary to get the map API key.

For Ubuntu users keystore is present in .android folder which is located in your home directory.

  • press ctrl+alt+t
  • Enter su and then your root password
  • Type:  keytool -list -alias androiddebugkey -keystore /home/ethics/.android/debug.keystore -storepass android -keypass android

in my case ethics is my user name.


androiddebugkey, Jul 1, 2011, PrivateKeyEntry,Certificate fingerprint (MD5): 55:EB:52:76:EE:11:3C:B7:90:1D:DF:6A:98:A0:32:0C

Note : .android will be a hidden file so press ctrl+h in your home directory to see it.

Copy this 55:EB:52:76:EE:11:3C:B7:90:1D:DF:6A:98:A0:32:0C and paste it in My certificate’s MD5 fingerprint edit box in this link . Now you will get an API key as an output. Copy the generated API key, this will be used in your Project.


Now go to eclipse and open a new project selecting Google APIs


Then edit the java file as bellow:

  • MapActivity is a class from which helps you to use map related operations.
  • isRouteDisplayed() is its override method so include that.


Alter your manifest file in the same order as mentioned bellow:

  • Since google maps library doesn’t belong to Android API you need to give library permission.
  • Google map uses Internet so you have to add Internet permission too.


android:name=”” />

<uses-permission android:name=”android.permission.INTERNET”/>

NOTE: Place it in the same order as mentioned bellow


Your Xml file has to be edited as bellow:

  • is a widget just like other views in android.
  • API key which you obtained from this site must be pasted under android:apiKey attribute.

Your final output:

That’s it we are done. Now you will be able to access the google map from your application.


Never miss place the tags in Manifest file it will create problem.

In case of any error look at this sites: