Wednesday, December 12, 2012

5 Great custom controls for Android

As an Android developer, one needs to make sure that the applications look good on all the devices irrespective of their sizes, manufacturer and OS versions. Here is a list of some great libraries that would make your applications look great.



ActionBar Sherlock

The library will automatically use the native action bar when appropriate or will automatically wrap a custom implementation around your layouts. This allows you to easily develop an application with an action bar for every version of Android from 2.x and up.


ViewPager Indicator

A great library to get view page indicators in multiple styles. A must have in you app if you a are planning to get rid of traditional tab-widgets.





Lock Pattern

Android has an useful tool in security settings, it is Lock Pattern. Users can define their own lock pattern ‒ which is a combination of 4+ dots and use it as the password to their device. This library extends the same functionality and enables you to make your app secure with a pattern lock. 

 Pull to Refresh

Twitter has it, Facebook has it and now your Android app can also have this cool feature! Just pull the listview to refresh the contents. Very simple to integrate and fun to use feature.











 CoverFlow

This is a simple implementation of android's cover flow    widget and is a cool feature for media and image heavy applications.

Friday, December 7, 2012

Tips and tricks for smarter development

Here is a compilation of the handy tips and tricks that i come across while developing for Android.The list is expected to grow with time and you can always post a comment to get something added to it :)

1. Go wireless

Install ADBwireless from playstore on your device and press the "Big Button" to enable wireless degubbing. Change to the platform-tools folder of the android-sdk and type the following command:
./adb connect 192.168.xxx.xxx:XXXX
Check for the device in the eclipse DDMS perspective and use it just like a normal connected device. Unfortunately it works on for rooted devices.

2. Have a memory management activity

The "Clear Data" button present by default in the "Manage Applications" clears the database and shared preferences for the application.If you do not want the users to clear all the data then this option can be overridden by adding a custom space-management activity in the application. Add the following line to Application tag:

 android:manageSpaceActivity="your.package.name.activityname"


3. Let the user choose install location

In the Android manifest file add the following line to the manifest tag and set the location of app installation. I always choose preferExternal so that apps can be moved to sd card, but that might not be a wise choice for game developers.

    android:installLocation="preferExternal"

4.  Not Enough space to install the application!

Do you really need to remove applications to install new ones? No you don't! Connect the device to adb and run the following command and install as many apps you want on the sdcard.

 ./adb shell pm setInstallLocation 2

5. Save time writing parsers , use Gson

 If you need to parse JSON responses from web services, you can save a lot of time by using Gson API. It is an android compatible JSON parser which parses the response and populates the beans with just tow lines of code!

6. Surprise!

Go to phone Settings > About phone  and tap multiple times on the Firmware version option. 

Wednesday, July 4, 2012

Google cloud Messaging (GCM) tutorial

At the Google IO 2012, the beta version of Android push notifications system called C2DM was replaced by Google Cloud Messaging (GCM). GCM has many new features over the existing system. Refer to this document for details of the differences and details about migrating your existing systems from C2DM to GCM.

To get started, download the Extras > Google Cloud Messaging for Android Library from the Android SDK.This library provides the jars to simplify the development on both the server side and client side.

From the Google API console page, create a new project and generate the API key. Refer this page for complete details.

Client Application

Create a new android project and add the GCM.jar to its buildpath.Generate the AndroidManifest.xml file as per the document. You can also verify that all the required parameters have been set in the manifest file by calling this function:
GCMRegistrar.checkManifest(this);
Create a new activity and add the following code to register. replace the SENDER_ID with the 12 digit project Id from the URL.
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
  GCMRegistrar.register(this, SENDER_ID);
} else {
  Log.v(TAG, "Already registered");
}

Once the app is created, run it on the android device and note the registrationId from the logs. Make sure that the device is having Android Market/Play Store installed and signed in using a Google Account. The code might not run on emulators.
Add a service to the project which would extend the GCMBaseIntentService and implement what needs to be done when on registration, de-registration, message receipt etc. On receiving a message, the data sent from server is in the form of an intent which can be resolved as :

@Override
protected void onMessage(Context arg0, Intent arg1) {
Log.d("GCM", "RECIEVED A MESSAGE");
// Get the data from intent and send to notificaion bar
generateNotification(arg0, arg1.getStringExtra("message"));
 }
In the generateNotification function, add the code that you need to do on receiving a message.In this example, I display the text in the notification bar.

Server Application
To test the push notifications, create a simple java project with a main function. Add the gcm-server.jar from the Android SDK extras folder to its build path.

Use the below code to send the notifications:

Sender sender = new Sender("AIzaXXXXXXXXXXXXXXXXXXXXXXXXX");
Message message = new Message.Builder().build();
Result result = sender.send(message,"device_token", 1);
System.out.println(result.toString());

To add some payload data into the notification, create the message using the below code.This message is sent as an intent which can be resolved at the client end.
Message message = new Message.Builder()
   .collapseKey("1")
   .timeToLive(3)
   .delayWhileIdle(true)
   .addData("message",
     "this text will be seen in notification bar!!")
   .build();
To multicast a message, use an List of device tokes. Use a MulticastResult Object in this case instead of the Result object as done earlier:
ArrayList
devicesList = new ArrayList();

devicesList.add("device_token_1");
devicesList.add("device_token_2");

MulticastResult result = sender.send(message, devicesList, 1);
sender.send(message, devicesList, 1);

Google provides the sample apps in the SDK by default, you may download a very basic version of them containing only the code discussed in this tutorial from here.
The same application is also available on play store here.

Few points to be taken care of:
1. If you use EmailId as the SENDER_ID, then you are actually registering for C2DM and not GCM. The notifications would not be received using this code although you. would be able to fetch the registrationId
2. In the above code, on receiving a notification,only a logcat entry is seen. You may edit the same to show the text on notification bar or perform any other action by editing the class which implements GCMBaseIntentService .
3.While uploading the application to Play Store, Navigate to "Services and API" section and Link the sender IDs to ensure proper functioning.

Updates:
1. Multicast Messages
2.Payload data from server
3.Receive Payload data and display as notificatoin

Sunday, March 25, 2012

Chrome to Phone for All Countries


Chrome to phone is a Google cloud to device messaging (a.k.a. C2DM) application which sends the links from your browser to your android device. To get it running, you need a Chrome extension that can be installed from chrome webstore and its android application from Google Play(Android market).

The android app for "Chrome to Phone" is location locked and is not available in markets for many countries.The uses from these countries can now enjoy this by downloading the apk file form here and installing on their phone.
Just make sure your phone is running on Android 2.2 or above!