From e874366f766b22920c6cf1493081f7f96214ebb7 Mon Sep 17 00:00:00 2001
From: Bill Gruber 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. 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. This tutorial assumes that you're using Eclipse. If you're not, see
-Developing in Other IDEs.
-You can then return to this tutorial and ignore anything about Eclipse. This tutorial assumes that you're using Eclipse. If you're using the command line, see
+Building and Running from the
+Command Line. You can then return to this tutorial and ignore anything about Eclipse. 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
@@ -43,12 +41,12 @@ you need to do it now. To install a platform in Eclipse: To learn more about how to use AVDs and the options
- available to you, refer to the
- Android
- Virtual Devices document. 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
+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:
The Create New AVD dialog appears.
- +The Create New AVD dialog appears.
+You can ignore the rest of the fields for now.
+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.
+After you've created an AVD, the next step is to start a new -Android project in Eclipse.
+After you've created an AVD you can move to the next step and start a new Android project in +Eclipse.
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 Project". (After you create one or more Android projects, an entry for "Android XML File" will also be available.)
Here is a description of each field:
- +Your package name must be unique across - all packages installed on the Android system; for this reason, it's + 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.
Other fields: The checkbox for "Use default location" allows you to change - the location on disk where the project's files will be generated and stored. "Build Target" - is the platform target that your application will be compiled against - (this should be selected automatically, based on your Min SDK Version).
- -Notice that the "Build Target" you've selected uses the Android 1.1 - platform. This means that your application will be compiled against the Android 1.1 - platform library. If you recall, the AVD created above runs on the Android 1.5 platform. - These don't have to match; Android applications are forward-compatible, so an application - built against the 1.1 platform library will run normally on the 1.5 platform. The reverse - is not true.
+ +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 Your Android project is now ready. It should be visible in the Package Explorer on the left. Open
+the Notice that the class is based on the {@link android.app.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
-{@link android.app.Activity#onCreate(Bundle) onCreate()} method
-will be called by the Android system when your Activity starts —
+ Notice that the class is based on the {@link android.app.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
+{@link android.app.Activity#onCreate(Bundle) 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 will.HelloAndroid.java
file, located inside HelloAndroid > src >
+HelloAndroid.java
file, located inside HelloAndroid > src >
com.example.helloandroid). It should look like this:
@@ -186,13 +189,13 @@ public class HelloAndroid extends Activity {
}
}
-
Now let's modify some code!
@@ -221,11 +224,12 @@ public class HelloAndroid extends Activity { }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.
+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 theimport
statements in your code for this to work.
An Android user interface is composed of hierarchies of objects called -Views. A {@link android.view.View} is a drawable object used as an element in your UI layout, +Views. A {@link android.view.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 {@link android.widget.TextView}.
@@ -237,7 +241,7 @@ 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 passthis
as your Context reference to the TextView.
-Next, you define the text content with +
Next, you define the text content with {@link android.widget.TextView#setText(CharSequence) setText()}.
Finally, you pass the TextView to @@ -277,7 +281,7 @@ and launches the default Activity. You should now see something like this:
The "Hello, Android" you see in the grey bar is actually the application title. The Eclipse plugin
creates this automatically (the string is defined in the res/values/strings.xml
file and referenced
-by your AndroidManifest.xml
file). The text below the title is the actual text that you have
+by your AndroidManifest.xml
file). The text below the title is the actual text that you have
created in the TextView object.
That concludes the basic "Hello World" tutorial, but you should continue reading for some more @@ -307,7 +311,7 @@ programmatically-constructed example:
android:text="@string/hello"/>The general structure of an Android XML layout file is simple: it's a tree -of XML elements, wherein each node is the name of a View class +of XML elements, wherein each node is the name of a View class (this example, however, is just one View element). You can use the name of any class that extends {@link android.view.View} as an element in your XML layouts, including custom View classes you define in your own code. This @@ -316,7 +320,7 @@ structure and syntax than you would use in a programmatic layout. This model is by the web development model, wherein you can separate the presentation of your application (its UI) from the application logic used to fetch and fill in data.
-In the above XML example, there's just one View element: the TextView
,
+
In the above XML example, there's just one View element: the TextView
,
which has five XML attributes. Here's a summary of what they mean:
This attribute assigns a unique identifier to the TextView element.
- You can use the assigned ID to reference this View from your source code or from other
+ You can use the assigned ID to reference this View from your source code or from other
XML resource declarations.
|
@@ -352,7 +356,7 @@ which has five XML attributes. Here's a summary of what they mean:
- This attribute defines how much of the available width on the screen this View should consume.
+ This attribute defines how much of the available width on the screen this View should consume.
In this case, it's the only View so you want it to take up the entire screen, which is what a value of "fill_parent" means. |
@@ -369,7 +373,7 @@ which has five XML attributes. Here's a summary of what they mean:
- This sets the text that the TextView should display. In this example, you use a string
+ This sets the text that the TextView should display. In this example, you use a string
resource instead of a hard-coded string value.
The hello string is defined in the res/values/strings.xml file. This is the
recommended practice for inserting strings to your application, because it makes the localization
@@ -396,17 +400,17 @@ such as images, sounds, and localized strings.
- The Eclipse plugin automatically creates one of these layout files for you: main.xml. -In the "Hello World" application you just completed, this file was ignored and you created a + The Eclipse plugin automatically creates one of these layout files for you: main.xml. +In the "Hello World" application you just completed, this file was ignored and you created a layout programmatically. This was meant to teach you more -about the Android framework, but you should almost always define your layout +about the Android framework, but you should almost always define your layout in an XML file instead of in your code. -The following procedures will instruct you how to change your +The following procedures will instruct you how to change your existing application to use an XML layout.
Now re-run your application — because you've created a launch configuration, all -you need to do is click the green arrow icon to run, or select +you need to do is click the green arrow icon to run, or select Run > Run History > Android Activity. Other than the change to the TextView string, the application looks the same. After all, the point was to show that the two different layout approaches produce identical results. -Tip: Use the shortcut Ctrl-F11 -(Cmd-Shift-F11, on Mac) to run your currently visible application. +Note: You may have to unlock the screen on the emulator to see +your application — just as you would unlock the screen on a device. If you have problems +running the emulator, see Using the +Android Emulator. Continue reading for an introduction to debugging and a little more information on using other IDEs. When you're ready to learn more, read Application -Fundamentals for an introduction to all the elements that make Android applications work. +Fundamentals for an introduction to all the elements that make Android applications work. Also refer to the Developer's Guide introduction page for an overview of the Dev Guide documentation. R class-In Eclipse, open the file named In Eclipse, open the file named @@ -510,16 +516,17 @@ public final class R { Press "Force Quit" to terminate the application and close the emulator window. -To find out more about the error, set a breakpoint in your source code
-on the line To find out more about the error, set a breakpoint in your source code
+on the line Creating the Project without Eclipse- +If you don't use Eclipse (such as if you prefer another IDE, or simply use text editors and command line tools) then the Eclipse plugin can't help you. Don't worry though — you don't lose any functionality just because you don't use Eclipse. - +The Android Plugin for Eclipse is really just a wrapper around a set of tools included with the Android SDK. (These tools, like the emulator, aapt, adb, - ddms, and others are documented elsewhere.) + ddms, and others are documented elsewhere.) Thus, it's possible to wrap those tools with another tool, such as an 'ant' build file. - +The Android SDK includes a tool named "android" that can be
used to create all the source code and directory stubs for your project, as well
as an ant-compatible For example, to create a HelloAndroid project similar to the one created in Eclipse, use this command: - +android create project \ --package com.example.helloandroid \ - --activity HelloAndroid \ + --activity HelloAndroid \ --target 2 \ - --path <path-to-your-project>/HelloAndroid + --path <path-to-your-project>/HelloAndroid- This creates the required folders and files for the project at the location + This creates the required folders and files for the project at the location defined by the path. - -For more information on how to use the SDK tools to create and build projects, please read + + For more information on how to use the SDK tools to create and build projects, please read Developing in Other IDEs. \ No newline at end of file -- cgit v1.1 |