Dive into Android development – III

Now that we are off the starting trouble in Android development, let’s develop a simple login page. We will create username/password fields with a login button. If the username is “Admin” and password is “Admin123” we will display a valid message, else we will display an error message. 
An unformatted UI is shown below:
Let’s create a new project “DiveIntoAndroid3” and complete the basic rituals discussed in the earlier post. To construct the UI you will use the main.xml file present in the res/layout folder. The UI design of Android applications are present in an XML file similar to Flex, Sliverlight etc. Opening the main.xml file will provide you a template, where you can drag and drop the UI components from the palette. You can switch between the design view and the code  by clicking the  tabs ‘Graphical Layout’ and ‘main.xml’ below.
Let’s drag and drop TextView and EditText components and a button. You can change the id of each component from the main.xml. We will play with the layout later. The final UI looks like below.


The ‘id’ atrributes of the components have been changed.
Now, let’s jump into some code in our Acitivity class. We can get reference to the components by using findViewById method. We can register a OnClickListener for the button and have the logic written as shown below.

public class DiveIntoAndroid3Activity extends Activity {
private EditText userNameTxt;
private EditText passwordTxt;
private TextView message;
private Button loginBtn;
    public void onCreate(Bundle savedInstanceState) {

        userNameTxt = (EditText)findViewById(;
        passwordTxt = (EditText)findViewById(;
        message = (TextView)findViewById(;
        loginBtn = (Button)findViewById(;
        loginBtn.setOnClickListener(new OnClickListener() {
              public void onClick(View view) {
 if(“Admin”.equals(userNameTxt.getText().toString())&&     “Admin123”.equals(passwordTxt.getText().toString()))
                   message.setText(“Credentials are valid”);
                 message.setText(“Credentials are invalid “);

Running this application will give you the simple login functionality that you are looking for.

Dive into Android development – II

We had Eclipse IDE with Android plugins configured in the earlier post. Let’s start with our traditional hello world example. The application should pop up a Hello World dialog box.
Let’s create a simple Android project in Eclipse, call it ‘DiveIntoAndroid2‘. Eclipse will ask for a target platform and we’ll choose ‘Android 2.2’. You will have to specify a root package name for the project and let that be ‘com.durasoft’. Clicking finish button will give you a project structure as shown below.
You will write the code in the file. An Activity in Android is equivalent to a window with all the UI components in it. So typically an Android application is nothing but a collection of activities and DiveIntoAndroid2Activity is one such activity which will be launched when you execute the application.
Let’s create a dialog box with the message “Hello World”.  There are a number of ways to implement this, but let’s take the simplest route. 
      AlertDialog.Builder alertDlgBuilder = new AlertDialog.Builder(this);
        AlertDialog alertDlg = alertDlgBuilder.create();
        alertDlg.setMessage(“Hello World”);;

If you have worked with Java Swing or AWT or WinForms working with UI components in Android will be a breeze. Let’s not add more code this time and we’ll just focus on executing it.
Execute the application by right clicking the project in project explorer and selecting ‘Run As’ Android application. The application will automatically launch the Android emulator that we had already created during the setup. 
The emulator will be launched and will take time to start and initialize, similar to starting a mobile device as shown below.
After you unlock by dragging the lock bar to the right side, the application get’s launched and you get the output as shown below.
I had created an AVD called ‘Froyo’,  targeting the Android 2.2 platform and had selected it as default. So when I ran the application Froyo got launched.
Btw what is Froyo?



Dive into Android development – I

After a series of posts on jQuery plugin I thought I should write a few on developing apps for the android platform. There are plenty of articles/books on android development, apart from the very good documentation on android provided by Google.
So, I want to keep the theory very minimal and just focus on implementing a variety of lightweight applications.  People who know Java (or even C# or any OO language) should be in a comfort zone.
Alright let’s get the recipe out for developing android apps. Here is what you need.
  • JDK 6 or above
  • Eclipse IDE for JEE developers. You can download it from to say, extract it and open it after you have downloaded it.
  • Android SDK. You can download it from   
  • Eclipse plugin for Android so that you can play with android applications from/within Eclipse. 
    • Open eclipse, go to Help->Install new software->Click ‘Add’->Type ADT in the Name textbox and in the location textbox.
    • Select the Developer Tools checkbox and click Next, again Next, Accept the licence agreements and finally hit Finish
    • Now that’s going to take 10-15 minutes of your time depending on the speed of your connection. You may have to restart eclipse after this process.
  • After restarting eclipse it may ask you to configure the android SDK. You can either choose to download a fresh copy of android SDK or give the location of Android SDK that you have downloaded earlier.
  • Android SDK is a fundamental starter package. Say, you’re developing an application that will run on    Android 3.0, then you need to have the Android 3.0 platform and tools installed.  Go to Window->Android SDK Manager you will see the screen below. You can select Android 3.0 or anything that suits your requirement and install. This is a time consuming process.
  •  The application that you will build will be run in an android device, but what if you have a different version of android phone or sometimes don’t even have it(yes, it’s quite common!!!)? You can configure an android emulator or Android Virtual Device. Go to window->AVD Manager. You can create new Android virtual device(s) targeting any platform. So the applications can be run in any of these during development. I have configured a Virtual Device called ‘HoneyComb‘ for Android 3.0 platform as shown below.

There you go!!! We are all set to develop simple android apps.
Btw what is HoneyComb? 


Grails, Sencha Touch and MVC

It’s pretty tedious implementing MVC pattern in Sencha Touch applications. Sencha Touch provides facilities for modularizing the JavaScript code using MVC, but not as easy as Grails does. Grails enforces MVC seamlessly.

The structure of a Grails application looks like below.

The structure of a Sencha Touch MVC application is given below.

Take a close look at the file structure in both the cases. You will find that the conventions used in both Grails and Sencha Touch MVC are very similar, thereby improving the understanding of both the frameworks much better.


jQuery Plugin – III

“fn” property in $ is used to attach new functions in jQuery. You can also develop plugins by using global functions. You can directly assign a new method to $ function as given below:
$.xxx = function(){

Say you want to capitalize a String you can write
$.capitalize = function(value){
 return value.toUpperCase();
You can use it by calling alert($.capitalize(“plugin”));
One useful method that I have always written,  is  while working with dates.
$.daysFromNow = function(days){
  var date = new Date();
  return date;
You invoke it as $.daysFromNow(7) it will return the next week’s date. 
I found the “daysFromNow” example from Venkat Subramaniam‘s  Programming Groovy book. He has implemented a “daysFromNow” function in Groovy using it’s meta-programming capability.
You can also implement a lot of functionalities that involve arrays, in this fashion. 
So crack some code and have fun!!!