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="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <layer-list >
             <item>
       <shape>
           <solid
               android:color="#a276eb" />
           <stroke
               android:width="3dp"
               android:color="#6a3ab2" />
           <corners
               android:radius="3dp" />
           <padding
               android:left="5dp"
               android:top="5dp"
               android:right="5dp"
               android:bottom="5dp" />
       </shape>
       </item>
       <item>
           <bitmap android:gravity="center_vertical|center_horizontal" android:src="@drawable/dollar" android:tileMode="disabled"/>
       </item>
     
        </layer-list>
    </item>
    <item android:state_checked="false">
        <shape>
            <gradient
                android:startColor="#a276eb"
                android:endColor="#672a7c"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#6a3ab2" />
            <corners
                android:radius="1dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

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:
android:background="@drawable/mybutton" 
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: stackoverflow.com/questions/llegalstateexception-when-replacing-a-fragment

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: http://www.vogella.com/articles/JAXB/article.html

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. http://simple.sourceforge.net/ 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 : http://labs.micromata.de/display/jak/HelloKML+%28Quickstart%29.

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) https://code.google.com/p/javaapiforkml/

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

1. http://stackoverflow.com/questions/13803833/map-api-v2-authorisation-failure/13805807#13805807
2. http://stackoverflow.com/questions/14497179/google-maps-android-api-v2-failed-to-load-map-could-not-contact-google-servers
3. http://stackoverflow.com/questions/15598596/google-maps-api-v2-failed-to-load-map-could-not-contact-google-servers-even

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

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.