Smart mobiles are gaining wider acceptance with each passing day. The market acceptance is resulting in increased demand for easier as well as flexible programming platforms, which in turn has resulted in new players entering the mobile SDK arena. The latest entrant is Google with its Android platform and corresponding SDK, which will be the focus of this discussion. The first section will be about whys and wherefores of Android. In the second section, the focus will be on the steps required in developing a basic Android application. The last section will focus on developing an application based on the steps described in the second section. That sets the agenda for this discussion.
Android – the Whys and Wherefores:
By definition “Android is a software stack for mobile devices that includes an operating system, middleware and key applications”. In other words, Android not only provides a runtime environment but also an SDK to develop applications aimed at the runtime environment. The Android stack consists of the following layers:
1. Kernel
2. Android Runtime
3. Libraries
4. Application Framework
5. Applications
The layers enumerated above are in bottom-up fashion i.e. Kernel forming the bottom most layer and Applications forming the topmost layer. Even though, Android is termed as a software stack, it, more or less exhibits characteristics of an Operating System that will be evident from details of the layers.
1. Kernel
It forms the foundation of Android. Kernel does all the low-level interactions at the hardware level. Kernel achieves this via the device drivers that are present at this layer. Android makes use of Linux kernel 2.6. The core services provided by the kernel include security, memory management, process management and network stack and driver model. In short, the kernel acts as a layer that encapsulates the interaction between other layers and hardware.
2. Android Runtime:
Runtime contains basic libraries that are required to run an application. These libraries provide most of the functionalities provided by Java libraries. Android applications target Dalvik virtual machine (VM), which essentially is the runtime. Each application runs in its own process, each with its own instance of Dalvik VM. The runtime depends on the kernel for functionalities such as system-level threading and low-level memory management.
3. Libraries:
The various components and sub-systems of Android are included in the stack as a set of C/C++ libraries. The library includes System library, Media libraries, Surface Manager Library, OpenGL library etc. The functionalities of these libraries are provided to the developer through the Application Framework Layer.
4. Application Framework:
This layer exposes the functionalities provided by the Libraries layer to the developer. The Libraries layer contains C/C++ API. The Application Framework layer wraps those API and exposes it to the developer as Java API. The Application Framework consists of components such as Activity Manager, Window Manager, Content Providers, View Systems etc. Applications are built upon the services provided by these components.
5. Applications:
This is topmost layer of the Android stack. This layer contains pre-built applications such as email client, SMS program, maps, browser etc. The custom applications deployed by the user sit in this layer.
That completes the bird-eye view of the Android. In the next section, the steps to develop an Android application will be enumerated. From here on, Android would mean the complete stack and Android SDK would mean the tools and libraries required for developing applications for Android.
Developing an Android Application – Step-by-Step:
An Android application can be developed either by using Eclipse plug-in or by using tools provided alongwith the SDK. In this discussion, the second way will be used. The advantage of using the second step is that one will not be tied up with a particular IDE. Following are the steps for developing application without using Eclipse:
1. Generate Project Directories and Stubs
2. Develop UI logic
3. Compile and Deploy the Project
For all the steps except second, SDK provide tools. Here are the details.
1. Generate Project Directories and Stubs:
First step is to generate the directory structure and the source code stubs. To achieve this end, Android SDK provides a tool named activityCreator.py. It is a Python script. The script also creates an ant build file that can later be used to compile the application. The option that one needs to keep in mind is the out option. Using the out option, the name of the project directory can be specified. For example, to create a HelloWorld project with ‘HelloWorld’ as the directory and org.me.android.hello.HelloWorld as the class, the command will be
activityCreator.py –out HelloWorld com.android.hello.HelloWorld
2. Develop UI logic:
Next step is to develop the UI of the application. The activityCreator.py script created the stubs. To develop the UI, the generated source-code stub needs to be edited. It can be found in the src folder of the generated application folder. For example, the stub generated for the HelloWorld can be found inside HelloWorld\src folder. The auto-generated class will be as follows
public class HelloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
}
}
The UI in Android is composed of hierarchies of Views. Each component is a view. Some of the views act as containers. One of the most commonly used views is TextView. It provides the functionality of a label. To instantiate an object of the TextView class, an instance of the Android Context is required.
Context is a handle to the system. It provides access to the services such as accessing database, resolving resource bundles to their locales etc. Activity class is a sub-class of Context class. Any class that is generated by the script inherits Activity. Hence, instance of Context can be passed by passing the instance of the generated class using this keyword. For example, to instantiate an object of TextView class named textView, the statement will be
TextView textView = new TextView(this);
Next step is to display a text value using the TextView. To do so, setText method of TextView class can be used. For example, the following statement will display “Hello World”
textView.setText(“Hello World”);
Last step in displaying the text label is to add instance of TextView to the onscreen display. setContentView() method of Activity class can be used to connect or add instance of TextView class. For example, to add textView to the onscreen display, the statement will be
setContentView(textView);
Next, let us see how to compile the project and deploy it.
3. Compile and Deploy the Project:
To compile the project, the only tool needed is ant. In the folder containing the
build.xml file, one has to just give the following command
ant
The ant tool will take the build.xml file and generate the class files. Then it will
package them into an .apk file. It is Android asset package file. The next step is to
deploy the .apk file. To do so, adb tool can be used. The install option of the adb
tool tells the tool to install the package into the simulator. The parameter that
needs to be passed to the tool is the path to the package.
That completes this section. Next, a small application will be developed based on the steps detailed in this section.
Android SDK – In Real World:
The application to be developed will have following features:
1. A widget, which will be a button
2. An event-handler for the button
3. A dialog box that will be shown when the button is clicked
The application, though simple, will be the base for future discussions. So lets get started. First step is to create the application structure. The name of the application will be AndroidNotepad. Using the createActivity.py the structure can be created. Once the structure is created, the AndroidNotepad.java will contain the following code
package org.me;
import android.app.Activity;
import android.os.Bundle;
public class AndroidNotepad extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(button);
}
}
Next, a button needs to be added. That can be done as shown in the code
package org.me;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class AndroidNotepad extends AndroidNotepad {
/** Called when the activity is first created. */
Button button;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
button =new Button(this);
setContentView(button);
}
}
The next step is to capture the click event of the button. To do so, the OnClickListener interface needs to be implemented. The interface can be found in View package. To implement the OnClickListener interface, one needs to override the onClick() method. The following code implements the OnClickListener for the button
package org.me;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.*;
public class AndroidNotepad extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
Button button;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
button =new Button(this);
setContentView(button);
}
public void onClick(View arg0) {
}
}
To attach the event handler to the button, the setOnClickListener() method of Button can be used.
package org.me;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.*;
public class AndroidNotepad extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
Button button;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
button =new Button(this);
button.setOnClickListener(this);
setContentView(button);
}
public void onClick(View arg0) {
}
}
Lastly, the code to display the dialog box when the button is clicked is as under
package org.me;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.*;
public class AndroidNotepad extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
Button button;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
button =new Button(this);
button.append(“Click”);
button.setOnClickListener(this);
setContentView(button);
}
public void onClick(View arg0) {
showAlert(“Button”, 0, “Button Clicked”, “OK”, true);
}
}
To show the dialog box showAlert method is used. More about the showAlert() method in the future discussions. That brings us to the end of this discussion. This discussion has just touched the tip of the iceberg that is Android. The next part of this discussion will be about using XML based definitions for the UI. Till then…
Smart mobiles are gaining wider acceptance with each passing day. The market acceptance is resulting in increased demand for easier as well as flexible programming platforms, which in turn has resulted in new players entering the mobile SDK arena. The latest entrant is Google with its Android platform and corresponding SDK, which will be the focus of this discussion. The first section will be about whys and wherefores of Android. In the second section, the focus will be on the steps required in developing a basic Android application. The last section will focus on developing an application based on the steps described in the second section. That sets the agenda for this discussion.
Android – the Whys and Wherefores:
By definition “Android is a software stack for mobile devices that includes an operating system, middleware and key applications”. In other words, Android not only provides a runtime environment but also an SDK to develop applications aimed at the runtime environment. The Android stack consists of the following layers:
1. Kernel
2. Android Runtime
3. Libraries
4. Application Framework
5. Applications
The layers enumerated above are in bottom-up fashion i.e. Kernel forming the bottom most layer and Applications forming the topmost layer. Even though, Android is termed as a software stack, it, more or less exhibits characteristics of an Operating System that will be evident from details of the layers.
1. Kernel
It forms the foundation of Android. Kernel does all the low-level interactions at the hardware level. Kernel achieves this via the device drivers that are present at this layer. Android makes use of Linux kernel 2.6. The core services provided by the kernel include security, memory management, process management and network stack and driver model. In short, the kernel acts as a layer that encapsulates the interaction between other layers and hardware.
2. Android Runtime:
Runtime contains basic libraries that are required to run an application. These libraries provide most of the functionalities provided by Java libraries. Android applications target Dalvik virtual machine (VM), which essentially is the runtime. Each application runs in its own process, each with its own instance of Dalvik VM. The runtime depends on the kernel for functionalities such as system-level threading and low-level memory management.
3. Libraries:
The various components and sub-systems of Android are included in the stack as a set of C/C++ libraries. The library includes System library, Media libraries, Surface Manager Library, OpenGL library etc. The functionalities of these libraries are provided to the developer through the Application Framework Layer.
4. Application Framework:
This layer exposes the functionalities provided by the Libraries layer to the developer. The Libraries layer contains C/C++ API. The Application Framework layer wraps those API and exposes it to the developer as Java API. The Application Framework consists of components such as Activity Manager, Window Manager, Content Providers, View Systems etc. Applications are built upon the services provided by these components.
5. Applications:
This is topmost layer of the Android stack. This layer contains pre-built applications such as email client, SMS program, maps, browser etc. The custom applications deployed by the user sit in this layer.
That completes the bird-eye view of the Android. In the next section, the steps to develop an Android application will be enumerated. From here on, Android would mean the complete stack and Android SDK would mean the tools and libraries required for developing applications for Android.
Developing an Android Application – Step-by-Step:
An Android application can be developed either by using Eclipse plug-in or by using tools provided alongwith the SDK. In this discussion, the second way will be used. The advantage of using the second step is that one will not be tied up with a particular IDE. Following are the steps for developing application without using Eclipse:
1. Generate Project Directories and Stubs
2. Develop UI logic
3. Compile and Deploy the Project
For all the steps except second, SDK provide tools. Here are the details.
1. Generate Project Directories and Stubs:
First step is to generate the directory structure and the source code stubs. To achieve this end, Android SDK provides a tool named activityCreator.py. It is a Python script. The script also creates an ant build file that can later be used to compile the application. The option that one needs to keep in mind is the out option. Using the out option, the name of the project directory can be specified. For example, to create a HelloWorld project with ‘HelloWorld’ as the directory and org.me.android.hello.HelloWorld as the class, the command will be
activityCreator.py –out HelloWorld com.android.hello.HelloWorld
2. Develop UI logic:
Next step is to develop the UI of the application. The activityCreator.py script created the stubs. To develop the UI, the generated source-code stub needs to be edited. It can be found in the src folder of the generated application folder. For example, the stub generated for the HelloWorld can be found inside HelloWorld\src folder. The auto-generated class will be as follows
public class HelloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
}
}
The UI in Android is composed of hierarchies of Views. Each component is a view. Some of the views act as containers. One of the most commonly used views is TextView. It provides the functionality of a label. To instantiate an object of the TextView class, an instance of the Android Context is required.
Context is a handle to the system. It provides access to the services such as accessing database, resolving resource bundles to their locales etc. Activity class is a sub-class of Context class. Any class that is generated by the script inherits Activity. Hence, instance of Context can be passed by passing the instance of the generated class using this keyword. For example, to instantiate an object of TextView class named textView, the statement will be
TextView textView = new TextView(this);
Next step is to display a text value using the TextView. To do so, setText method of TextView class can be used. For example, the following statement will display “Hello World”
textView.setText(“Hello World”);
Last step in displaying the text label is to add instance of TextView to the onscreen display. setContentView() method of Activity class can be used to connect or add instance of TextView class. For example, to add textView to the onscreen display, the statement will be
setContentView(textView);
Next, let us see how to compile the project and deploy it.
3. Compile and Deploy the Project:
To compile the project, the only tool needed is ant. In the folder containing the
build.xml file, one has to just give the following command
ant
The ant tool will take the build.xml file and generate the class files. Then it will
package them into an .apk file. It is Android asset package file. The next step is to
deploy the .apk file. To do so, adb tool can be used. The install option of the adb
tool tells the tool to install the package into the simulator. The parameter that
needs to be passed to the tool is the path to the package.
That completes this section. Next, a small application will be developed based on the steps detailed in this section.
Android SDK – In Real World:
The application to be developed will have following features:
1. A widget, which will be a button
2. An event-handler for the button
3. A dialog box that will be shown when the button is clicked
The application, though simple, will be the base for future discussions. So lets get started. First step is to create the application structure. The name of the application will be AndroidNotepad. Using the createActivity.py the structure can be created. Once the structure is created, the AndroidNotepad.java will contain the following code
package org.me;
import android.app.Activity;
import android.os.Bundle;
public class AndroidNotepad extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(button);
}
}
Next, a button needs to be added. That can be done as shown in the code
package org.me;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class AndroidNotepad extends AndroidNotepad {
/** Called when the activity is first created. */
Button button;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
button =new Button(this);
setContentView(button);
}
}
The next step is to capture the click event of the button. To do so, the OnClickListener interface needs to be implemented. The interface can be found in View package. To implement the OnClickListener interface, one needs to override the onClick() method. The following code implements the OnClickListener for the button
package org.me;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.*;
public class AndroidNotepad extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
Button button;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
button =new Button(this);
setContentView(button);
}
public void onClick(View arg0) {
}
}
To attach the event handler to the button, the setOnClickListener() method of Button can be used.
package org.me;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.*;
public class AndroidNotepad extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
Button button;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
button =new Button(this);
button.setOnClickListener(this);
setContentView(button);
}
public void onClick(View arg0) {
}
}
Lastly, the code to display the dialog box when the button is clicked is as under
package org.me;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.*;
public class AndroidNotepad extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
Button button;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
button =new Button(this);
button.append(“Click”);
button.setOnClickListener(this);
setContentView(button);
}
public void onClick(View arg0) {
showAlert(“Button”, 0, “Button Clicked”, “OK”, true);
}
}
To show the dialog box showAlert method is used. More about the showAlert() method in the future discussions. That brings us to the end of this discussion. This discussion has just touched the tip of the iceberg that is Android. The next part of this discussion will be about using XML based definitions for the UI. Till then…
