Writing your first Android Application.

Posted by Anandhan Subbiah on Oct 8, 2008 in Java, Technical Articles14 comments

Setting Up Your Development Environment

Android applications, like most mobile phone applications, are developed in a host-target development environment. In other words, you develop your application on a host computer (where resources are abundant), and download it to a target mobile phone for testing and ultimate use.

To write your own Android mobile phone applications, you’ll first need to collect the required tools and set up an appropriate development environment on your PC or Mac.

The Android SDK supports several different development environments. For this book we will focus on using Eclipse, because it is the best integrated with the SDK, and, hey, it’s free. No matter which operating system you are using, you will need essentially the same set of tools:

* The Eclipse Integrated Development Environment
* Sun’s Java Development Kit (JDK)
* The Android Software Developer’s Kit (SDK)
* A special Eclipse plug in: the Android Developer Tool (ADT)

Creating an Android Development Environment

The Android Software Development Kit supports Windows (XP and Vista), Linux (Ubuntu Dapper Drake), and Mac OS X (10.4.8 or later) as host development environments. Installation of the SDK is substantially the same for any of the operating systems, and most of this description applies equally to all of them. Where the procedure differs, we will clearly tell you what to do for each environment.

1.Install Eclipse:
2.Check for Required Plugins
3.Install JDK: The Android SDK requires JDK version 5 or version 6.
4. Install Android SDK: This is where you’d start if you already have the right versions of Eclipse and the JDK loaded. The Android SDK is distributed through Google’s code site, http://code.google.com/android/download.html.
5. Update Environment Variables: To make it easier to launch the Android tools, add the tools directory to your path.
6. Install the Android Plugin (ADT): https://dl-ssl.google.com/android/eclipse.

Starting a New Android Application: HelloWorld

Several components are needed to build an Android application. Fortunately, the Eclipse IDE with the Android plug-in automates a lot of the work needed to create and maintain these components. We will start by using the IDE to create a project for our application. Start up Eclipse and select “File -> New -> Project…” from the menu bar (be sure to select “Project…,” not “Java Project”).

Select “Android Project” and click “Next” to get the “New Android Project” dialog box.

We’ll use “HelloWorld” as the name for both the Project and the Application. You don’t need to change the button or checkbox selections, and we’ll use the package name com.oreilly.helloworld as shown.

Every Android application has to have at least one Activity (an executable that usually has a user interface), so let’s say we’re going to include an Activity called HelloWorldActivity, as shown in the dialog box. Click “Finish,” and the Android Development Kit does a number of things for you, to make your life easier as a developer. In [figure to come], I’ve expanded the tree in the Package Explorer window to show some of the files and directories that the ADK created.

The ADK created a HelloWorld directory in the default Eclipse workspace for your project. It also created subdirectories for your source files (.src), references to the Android Library, assets, resources (.res), and a manifest file (AndoidManifest.xml). In each of the subdirectories it created another level of subdirectories as appropriate. Let’s take a quick look at them:

Sources (under .src)

* Contains a directory structure that corresponds to the package name you gave for your application: in this case, com.android.helloworld.
*Contains a Java template for the Activity you indicated was in the application (HelloWorldActivity) and a directory of resource references (R.java). We’ll come back to R.java later.

Android Library

This is just what it says. If you like, you can expand the android.jar tree and see the names of the modules included in the library. This is where your application will go for Android library references.

assets

Files you want to bundle with your application. We won’t have any for HelloWorld.

Resources (under .res)

* Drawable resources are any images, bitmaps, etc. that you need for your application. For HelloWorld, the ADK has supplied us with the default Android icon, and that’s all we’ll need.
* Layout resources tell Android how to arrange items on the screen when the application runs. These resources are XML files that give you quite a bit of freedom in laying out the screen for different purposes. For HelloWorld, we’ll just use the defaults generated by the ADK
* Values are constants, strings, etc. available for use by your application. Keeping them outside the sources makes it easier to customize the application, such as adapting it for different languages.

Manifest (AndroidManifest.xml)

This is another XML file that tells the Android build system what it needs to know to build and package your application so it can be installed on an Android phone or the emulator. This file has its own specialized editor, which we’ll describe as we get to more complicated applications.

Double Click on HelloWorldActivity.java
Looking quickly at the template code that the ADK has provided for us, we can note several things:

*The ADK has included the package reference we asked for, which is consistent with the directory structure it created.
*It has also created a (collapsed) set of imports for the library references it knows we need.
*It created a class definition for the Activity we said we wanted (HelloWorldActivity) including a method called OnCreate.

For the moment, don’t worry about the parameter passed into OnCreate. The icicle Bundle is a way of passing data between activities and storing data between instantiations of the same Activity. We won’t need to use this for HelloWorld.
* One special line of code has been included in OnCreate:

setContentView (R.layout.main);

Remember that Android uses layouts to define screen layouts on the target, and that main.xml was the name of the default layout file that the ADK created for us under .res/layout. The R.java file is generated automatically and contains java references for each of the resources under .res. You will never need to edit the R.java file by hand— the ADK takes care of it as you add, change or delete resources.

Double Click on main.xml
Again, let’s look at the key features of this template code:

* Like any other XML file, this one starts with a reference to the XML version and encoding used.
* LinearLayout is one of the screen layout formats provided by the ADK. There are several others, which can be combined hierarchically to create very complex screen layouts. For our purposes, a simple linear layout is fine.

Within the LinearLayout definition:
o The code:

xmlns:android=”http://schemas.android.com/apk/res/android”

identifies the XML schema being used.
o The code:

android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”

defines an orientation, width and height for the entire scope of the layout.
*TextView describes how to handle text within…. It resembles the text boxes you may have encountered when programming in other graphical environments.

Within the TextView definition:
o The code:

android:layout_width=”fill_parent”
android:layout_height=”wrap_content”

a width and height for the TextView box.
o The code:

android:text=”Hello World, HelloWorldActivity”

provides some text to display in the TextView. By a stroke of luck, the ADK has already included text that is almost what we wanted to display anyway. Just to show them who’s boss, change the android:text line to say “Hello, Android!”, or something else equally clever:

android:text=”Hello, Android!”

if you edit the file, save it either from the Eclipse File menu (File -> Save) or by clicking on the diskette icon in the menu bar.

Believe it or not, we’re done. We don’t have to write a single line of Java to create this application.

From the Eclipse menu bar, select Run -> Run. A “Run As” dialog box will pop up. Select “Android Application” from the list and you can see your first Android Application.

Reference : Android Application Development, 1st Edition
By: Rick Rogers

Tags: ,

14 comments

» Comments RSS Feed
  1. I wish to point out this article.

    Android First Example in Java: Hello World
    http://programmaremobile.blogspot.com/2009/01/android-first-example-hello-world.html

    It expain the steps(from java code to running into emulator) to make the first hello world without eclipse.
    I hope it is welcome!

  2. Here is a complete steps from configuring android environment to writing simple hello world program this is the best article i came across and it worked for me without a htich

    http://www.ceveni.com/2009/06/writing-android-hello-world-program.html

  3. Here is a helpful tutorial on setting Android SDK 2.1 and Eclipse 3.5

    http://androidcodemonkey.blogspot.com/2010/01/how-to-setup-android-development_23.html

    And a tutorial writing your first Hello World applications

    http://androidcodemonkey.blogspot.com/2010/01/hello-world-your-first-android.html

  4. Hey I have no idea where ta even start maken an app for the android is there any1 out there that could help me out…… alanwilshisen@gmail.com
    Ppzzz help me out..

  5. Great article but to be honest this is a “set up android” not really writing anything. However I just followed it to set up my development environment so thank you in the end.

  6. Hi,
    How can I set the video screen/object vertically in the screen.

  7. Its really very helpful…

  8. Android is everywhere. :)

  9. Thanks for the tutorial but recently I found something much easier for creating
    Android Apps and the best thing is that you don’t much programming skills to
    create your Android App , just focus on the logic
    http://bestfromgoogle.blogspot.com/2011/05/create-your-first-android-app-you-donot.html

  10. Wonderful… Easy… This s wat I needed..

  11. Nice article about setting up environment for android app development

  12. great explanations than

  13. great explanations thanks

  14. Thank you. :)

Leave a comment