Custom Search

Monday, June 22, 2009

Understanding the sample hello world android app

Development Environment

The Android SDK supports several different development environments. I have experimented using Eclipse, so I will be talking about it in this blog. Also, it is very well integrated with the android SDK, and it’s free :-).




To start developing with Eclipse you will require these tools:




Sun’s Java Development Kit (JDK)


The Android SDK


Eclipse (I have been using Eclipse Ganymade)


A special Eclipse plug in: the Android Developer Tool (ADT)



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.myapp.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, Click “Finish,” and the Android Development Kit does a number of things for you, to make your life easier as a developer. In the Package Explorer window you will see 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).


Android 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 an 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 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.

1 comment:

  1. This blog is looking good and i likes your background color, so you can add more information in this same blog and most of them get benefits from this blog.

    Android app developer

    ReplyDelete