Search This Blog

Wednesday, 7 March 2012

chapter1: How to display Simple message-"hello"


1)

Hello, World

In this document

As a developer, you know that the first impression of a development framework is how easy it is to write "Hello, World." Well, on Android, it's pretty easy. It's particularly easy if you're using Eclipse as your IDE, because we've provided a great plugin that handles your project creation and management to greatly speed up your development cycles.
.
Before you start, you should already have the SDK installed, and if you're using Eclipse, you should have installed the ADT plugin as well. If you have not installed these, see Installing the Android SDK and return here when you've completed the installation.

Create an AVD

To learn more about how to use AVDs and the options available to you, see Managing Virtual Devices.
In this tutorial, you will run your application in the Android Emulator. Before you can launch the emulator, you must create an Android Virtual Device (AVD). An AVD defines the system image and device settings used by the emulator.
To create an AVD:
  1. In Eclipse, select Window > Android SDK and AVD Manager.
  2. Select Virtual Devices in the left panel.
  3. Click New....
The Create New AVD dialog appears.
  1. Type the name of the AVD, such as "my_avd".
  2. Choose a target.
The target is the platform (that is, the version of the Android SDK, such as 2.3.3) you want to run on the emulator. For this tutorial, choose the latest platform that you have installed and ignore the rest of the fields.
  1. Click Create AVD.

Create a New Android Project

After you've created an AVD you can move to the next step and start a new Android project in Eclipse.
  1. In Eclipse, select File > New > Project....
If the ADT Plugin for Eclipse has been successfully installed, the resulting dialog should have a folder labeled "Android" which should contain "Android Project". (After you create one or more Android projects, an entry for "Android XML File" will also be available.)
  1. Select "Android Project" and click Next.
·  Fill in the project details with the following values:
  • Project name: HelloAndroid
  • Build Target: Select a platform version that is equal to or lower than the target you chose for your AVD.
  • Application name: Hello, Android
  • Package name: com.example.helloandroid (or your own private namespace)
  • Create Activity: HelloAndroid
Click Finish.

Here is a description of each field:
Project Name
This is the Eclipse project name — the name of the directory that contains the project files.
Build Target
This is the version of the Android SDK that you're using to build your application. For example, if you choose Android 2.1, your application will be compiled against the Android 2.1 platform library. The target you choose here does not have to match the target you chose for your AVD; however, the target must be equal to or lower than the target you chose for your AVD. Android applications are forward-compatible, which means an application will run on the platform against which it is built as well as all platforms that are released in the future. For example, an application that is built against the 2.1 platform library will run normally on an AVD or device that is running the 2.3.3. The reverse is not true.
Application Name
This is the human-readable title for your application — the name that appears on the Android device.
Package Name
This is the package namespace (following the same rules as for packages in the Java programming language) that you want all your source code to reside under. This also sets the package name under which the stub Activity is generated.
Your package name must be unique across all packages installed on the Android system; for this reason, it's important to use a standard domain-style package for your applications. The example above uses the "com.example" namespace, which is a namespace reserved for example documentation — when you develop your own applications, you should use a namespace that's appropriate to your organization or entity.
Create Activity
This is the name for the class stub that is generated by the plugin. This is a subclass of Android's Activity class. An Activity is simply a class that can run and do work. It can create a UI if it chooses, but it doesn't need to. As the checkbox suggests, this is optional, but an Activity is almost always used as the basis for an application.
Min SDK Version
This value specifies the minimum API Level on which your application will run. The Min SDK Version should be the same as the Build Target you chose. For example, if the Build Target is Android 2.1, then the Min SDK Version should be 7 or lower (it can never be higher than 7). For more information, see Android API Levels.
Other fields: The checkbox for "Use default location" allows you to change the location on disk where the project's files are generated and stored.
Your Android project is now ready. It should be visible in the Package Explorer on the left. Open the HelloAndroid.java file, located inside HelloAndroid > src > com.example.helloandroid). It should look like this:
package com.example.helloandroid;



import android.app.Activity;

import android.os.Bundle;



public class HelloAndroid extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

    }

}
Notice that the class is based on the Activity class. An Activity is a single application entity that is used to perform actions. An application may have many separate activities, but the user interacts with them one at a time. The onCreate() method is called by the Android system when your Activity starts — it is where you should perform all initialization and UI setup. An activity is not required to have a user interface, but usually does.
Now let's modify some code!

Construct the UI

package com.example.helloandroid;



import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;



public class HelloAndroid extends Activity {

   /** Called when the activity is first created. */

   @Override

   public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       TextView tv = new TextView(this);

       tv.setText("Hello, Android");

       setContentView(tv);

   }

}
Tip: An easy way to add import packages to your project is to press Ctrl-Shift-O (Cmd-Shift-O, on Mac). This is an Eclipse shortcut that identifies missing packages based on your code and adds them for you. You may have to expand the import statements in your code for this to work.
An Android user interface is composed of hierarchies of objects called Views. A View is a drawable object used as an element in your UI layout, such as a button, image, or (in this case) a text label. Each of these objects is a subclass of the View class and the subclass that handles text is TextView.
In this change, you create a TextView with the class constructor, which accepts an Android Context instance as its parameter. A Context is a handle to the system; it provides services like resolving resources, obtaining access to databases and preferences, and so on. The Activity class inherits from Context, and because your HelloAndroid class is a subclass of Activity, it is also a Context. So, you can pass this as your Context reference to the TextView.
Next, you define the text content with setText().
Finally, you pass the TextView to setContentView() in order to display it as the content for the Activity UI. If your Activity doesn't call this method, then no UI is present and the system will display a blank screen.
There it is — "Hello, World" in Android! The next step, of course, is to see it running.

Run the Application

The Eclipse plugin makes it easy to run your applications:
  1. Select Run > Run.
  2. Select "Android Application".

No comments:

Post a Comment