Advertisment

How to build mobile apps with Android?

author-image
CIOL Bureau
Updated On
New Update

BANGALORE, INDIA: The whole of last year there were speculations about Google coming up with its own mobile phone, termed as GPhone. But last November, the air was cleared of all rumors when Open Handset Alliance (OHA) released Android, a complete set of software for mobile devices that includes an operating system, middleware and key applications.

Advertisment

The OHA is a group of 34 leading multinational companies, including leading mobile operators, handset manufacturers, software companies, semiconductor companies and commercial companies. They had teamed up to develop a mobile phone that could give richer and more cohesive experience to users. And Google, as a member of the alliance has released Android SDK for the Android platform. Using this SDK, developers would be able to create mobile applications that would be able to harness all features of a handset.

Direct Hit!

Applies To: Java developers

USP: Platform for developing rich mobile apps

Primary Link: http://code.google.com/android/download.html

Keywords: Google Android

On CD: developers\android

For example, apps could be created to call upon a phone's core functionality like making calls, sending SMSes, accessing the calendar or even using the camera as a utility. In this article, we will tell you about Android and how to use it for developing mobile applicatoins.

Advertisment

Getting started

The Android SDK has been distributed with this month's PCQ_Professional CD. In Windows, only XP and Vista are supported. Other platforms supported by Android SDK are Linux and Mac OS X. To start application development you would also need Java SDK and Eclipse IDE. Android requires JDK 5 or later to work. Previous versions of JDK and even the Java Runtime Environment are not sufficient for it. Plus, it supports Eclipse 3.2 or 3.3 (Europa) for developing the mobile apps. Once you have installed your JDK and configured Eclipse IDE, you can straightaway start up with Android development. For this article we used JDK 6 and Europa as the IDE on a Vista machine.

Advertisment

Extract the contents of the zipped Android SDK to a folder. When you expand the directory you will find three folders: samples, docs and tools. You have to give the same directory path to the Tools folder as the path for environment variables. Once you have configured the path entry for system environment variables, you can start configuring the Android plugin for Eclipse. Start Eclipse and from Help > Software Updates > Find and Install, you can download and install the Android plugin. In the next window select the option 'Search for New features to Install,' and click on Next. Then on Install window, select New Remote Site and in the New Update Site dialog box enter the name as Android and URL as follows:

Select the checkbox for Android development tools, which will download and install Android plugins for Eclipse IDE
Advertisment

https://dl-ssl.google.com/android/ eclipse/

Click OK and the Android entry is added to the search list on the main window. Now press Finish to proceed with the search for the Eclipse updates. In the subsequent Search Result dialog box, select the checkbox Android > developer > Android Development Tools (ADT) and then click Next to proceed with the download. In the subsequent window accept the agreement and click Next button, while in the last window press Finish.

The ADT is not signed and while installation asks for acceptance, select 'Install All' to complete the download and installation process. Once installed, Eclipse restarts for the plugin to configure up. Now we can proceed up with Android development process within the Eclipse IDE, but before that we need to set Eclipse Preferences to point to the Android SDK directory. This can be configured by selecting Window > Preferences, and later clicking on Android on the left panel. In the main panel, browse for the SDK's location and then press OK to configure Android for Eclipse preferences. Now we can start with the development of an Android based application.

Advertisment

 

Building an application

Advertisment

Through the Android documentation provided along with the SDK you can start up with a 'Hello World' kind of application. Here we would try to explore a little more of the SDK framework; about the layout of UIs and Views and Activities. To start developing the demo application, first create a new Android Project by selecting File > New > Project menu. On the New Android Project window name the project as DemoAndroid, fill the Properties values for Package name, Activity name and Application name as shown in the visual.

Upon clicking the Finish button, the Android plugin runs and the auto-generated code for the project DemoAndroid gets created. The DemoAndroidApp class will be extending Activity class. An Activity is a thing that interacts with the user and is responsible for a window being created on which the UI components are placed. There are two methods that each subclass of Activity would implement:

On the Android Project window, name the project and also complete the Properties frame by giving names to Package, Activity and Application entries

Advertisment

onCreate(Bundle) :- It initializes the activity and by method setContentView() it describes the layout resource that defines the User Interface (UI). The method findViewById() is used to retrieve the UI components in that UI with which you want to interact with programmatically.

onPause() :- You override this method when you are dealing with a user who is leaving your activity, and you have to commit all changes done by the user before he leaves the activity.

Along with the DemoAndroidApp class you will find R.java file. It is an index for all the resources defined in the file. You use this class in your source code as a sort of short-hand way to refer to resources you've included in your project. We know that the method setContentView() looks for a resource that defines the UI. This definition file is an XML file and can be found in res directory under the main project directory. With res directory you can see there are directories named layout, drawable and values. The layout directory holds the main.xml file, which will be our UI definition file for the project. Other two directories are: Drawable, which holds the resource definition for the UI components design scheme; and 'Colors while values,' that holds values for UI components, be it a color or a string.

For our demo application we intend to ask user to enter his name and upon clicking the submit button, a message gets displayed that welcomes the user. For this we need an inputbox and a few text labels and a button. In Android we have EditText in place of inputbox and TextView to act as labels. The default layout is LinearLayout, that we'll use and add the following code for defining the UI before the LinearLayout attribute gets closed:



android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Enter your Name: -"/>

android:layout_width="fill_parent"

android:layout_height="35dip"

android:capitalize="sentences"

android:layout_weight="1" >





.

.

.

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="Submit" />

 

This way we have defined few TextViews for displaying the text and an EditText to take input from the user. Now we can programmatically call the layout and use the components to ask user for an input and upon selecting the button he gets a response. The following code snippet for DemoAndroidApp shows how, using the setContentView() method, we call upon the layout defined in main.xml and also how components like EditBox are being called upon by referring their ids as defined in main.xml file.

To run your app, open the Run window and select the project and its corresponding activity to execute on emulator



public class DemoAndroidApp extends Activity {

private EditText inputbox;

private TextView ans;

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.main);

inputbox = (EditText) findViewById(R.id.editor);

inputbox.setText("");

ans = (TextView) findViewById(R.id.ans);



final Button button = (Button) findViewById(R.id.back);

button.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) {

ans.setText("Welcome!! "+inputbox.getText());

inputbox.setText("");

}

});

}

}

We associate a listener to the button so that when the user clicks on the button he gets a welcome message as a response. Now we can run and execute the application.

Executing on Android emulator

Once you are done with coding, you can start running your application by selecting Run > open Debug Dialog. In the following Debug window, from the left pane, select the Android application and a new launcher entry appears with the name New_Configuration. Change the name of launcher to demoApp, and then under Android tab browse and select the current project. From the Activity option select the activity, which in our case is DemoAndroidApp. On pressing the Run button, the debugger starts the Android emulator, on which the application gets executed.

This way we have run a simple app where we defined the UI. You can refer the Android's documentation for more sample applications and other APIs. The complete source code for this project can be found on PCQ Forums under Current Issue thread.

tech-news