Thursday, April 25, 2013

Toggle button with Gradient and Image, Android

To create an android toggle button with a bitmap image and gradient follow the following steps:
1. Create new xml file in drawable-mdpi folder say mybutton.xml.
2. Add gradient and image to this file with selector and layer-list based on behavior you want to achieve. For example:

<selector xmlns:android="">
    <item android:state_checked="true">
        <layer-list >
               android:color="#a276eb" />
               android:color="#6a3ab2" />
               android:radius="3dp" />
               android:bottom="5dp" />
           <bitmap android:gravity="center_vertical|center_horizontal" android:src="@drawable/dollar" android:tileMode="disabled"/>
    <item android:state_checked="false">
                android:angle="270" />
                android:color="#6a3ab2" />
                android:radius="1dp" />
                android:bottom="10dp" />

3. In activity or Fragment where you want to place this button, add a normal button and set background to the custom view that we have defined above as:
and you are all set.

Thursday, April 11, 2013

Using multiple fragments and replacing/swapping with other fragments eventually, android

I found this very interesting post on stackoverflow about how to use multiple fragments and replace fragments on the go:

Excerpt: While using multiple fragments don't declare them in layout (or activity which will have fragment) instead add fragment to an activity programmatically and swap/switch with other fragments by fragment transaction manager. In case you define fragment in the XML of activity then it won't allow you to switch fragments dynamically (ofcourse by Java code) because once you define any fragment in XML that fragment is like static for that activity.

Very interesting thought while designing UI of your application.

Tuesday, April 9, 2013

XML, KML database for android

While working on an android application development, I researched about the possibilities of database for an android application to store geolocation of various points. During the research, first I started with keeping an XML (not KML) file with all values and use JAXB for reading and writing the XML. Following links are wonderful resources for learning XML access using JAXB:

However later I realized that you should not use JAXB for android app. It will increase the size of application by 9MB.

Then a work around of using XML in android app using SIMPLE. and it is a wonderful way to use XML for android.

Meanwhile following tutorial will help you to learn about KML in android. KML is the best way for storing geolocation in an android app but there are not major dummy application to get an understand of how to use it. Here is a link for simple usage of KML :

As JAXB is an api for accessing and modifying an XML file in java similarly we have a JAK (Java Api for KML) to access and update KML. Here is the code and more details about the JAK project (open-sourced)

I am not done with the application database and would update the post once I got go ahead with the best strategy.

 Update: Finally we decided on to use SQLite itself. Also got to know from some people that KML is not a database :O

Sunday, April 7, 2013

Google Maps Android API v2. Failed to load Map. Could not connect to Google Servers

If you are facing this error then follow these post thoroughly and you will definitely end up running your application. Though it took almost half day to get it fixed.

update: Complete tutorial for setting up Google Maps Android API2 : Google doc Android Map Api setup steps complete


In case you need reference, here is the link to my Android.manifest file for a dummy Map test application. Github Link:

NEXUS 4 device not detected in ADB devices list, eclipse, android development

If this is the first time you are trying to connect your NEXUS 4 (or any other google device) to eclipse for application deployment then you need to do following steps in order to have successful driver installation. (Sources: Multiple posts on stack overflow and other user-community)

1. Open 'SDK Manager' (from 'adt-bundle' directory).
2. Go to the bottom of list and install 'Google USB driver'.
3. In case you have already tried to install USB driver for your device then we first need to uninstall them. Go to device manager (right click "My Computer" > Device Manager). Then select "<device name>" from Portable devices tab. Right click and select uninstall driver.
4. Now on your phone, enable USB debugging. To enable, Go to "about phone", tap 7 times on "build number".
5. Connect phone to your machine and after connection, select "CAMERA (PTP)" on phone. This is important.
6. System will install the driver for the new device.
7. Again go to device manager and select the <device name> device under "Portable devices" tab. Select 'Update Device driver' and provide the manual path to search for driver. Path is : <android-sdk-directory>\extras\google\usb_driver.
8. Let it install the driver and android composite driver will be installed on your device.
9. Now open eclipse and start deploying apps to your phone.