From 39871b7e4368b9789e715dde5ef4ff9e891380cf Mon Sep 17 00:00:00 2001
From: Scott Main The graphical user interface for an Android app is built using a hierarchy of {@link
android.view.View} and {@link android.view.ViewGroup} objects. {@link android.view.View} objects are
-usually UI widgets such as a button or text field and {@link android.view.ViewGroup} objects are
+usually UI widgets such as buttons or
+text fields and {@link
+android.view.ViewGroup} objects are
invisible view containers that define how the child views are laid out, such as in a
grid or a vertical list. Android provides an XML vocabulary that corresponds to the subclasses of {@link
-android.view.View} and {@link android.view.ViewGroup} so you can define your UI in XML with a
-hierarchy of view elements.This lesson teaches you to
-
You should also read
-
-
-
+
@@ -39,63 +38,68 @@ next.link=starting-activity.html
Separating the UI layout into XML files is important for several reasons, -but it's especially important on Android because it allows you to define alternative layouts for +
Declaring your UI layout in XML rather than runtime code is useful for several reasons, +but it's especially important so you can create different layouts for different screen sizes. For example, you can create two versions of a layout and tell the system to use one on "small" screens and the other on "large" screens. For more information, see the class about Supporting Different -Hardware.
+Devices.
+
Figure 1. Illustration of how {@link -android.view.ViewGroup} objects form branches in the layout and contain {@link +android.view.ViewGroup} objects form branches in the layout and contain other {@link android.view.View} objects.
-In this lesson, you'll create a layout in XML that includes a text input field and a +
In this lesson, you'll create a layout in XML that includes a text field and a button. In the following lesson, you'll respond when the button is pressed by sending the content of the text field to another activity.
-Open the main.xml file from the res/layout/
-directory (every new Android project includes this file by default).
Open the activity_main.xml file from the res/layout/
+directory.
Note: In Eclipse, when you open a layout file, you’re first shown -the ADT Layout Editor. This is an editor that helps you build layouts using WYSIWYG tools. For this -lesson, you’re going to work directly with the XML, so click the main.xml tab at +the Graphical Layout editor. This is an editor that helps you build layouts using WYSIWYG tools. For this +lesson, you’re going to work directly with the XML, so click the activity_main.xml tab at the bottom of the screen to open the XML editor.
-By default, the main.xml file includes a layout with a {@link
-android.widget.LinearLayout} root view group and a {@link android.widget.TextView} child view.
-You’re going to re-use the {@link android.widget.LinearLayout} in this lesson, but change its
-contents and layout orientation.
The BlankActivity template you used to start this project creates the
+activity_main.xml file with a {@link
+android.widget.RelativeLayout} root view and a {@link android.widget.TextView} child view.
First, delete the {@link android.widget.TextView} element and change the value +
First, delete the {@link android.widget.TextView <TextView>} element and change the {@link
+ android.widget.RelativeLayout <RelativeLayout>} element to {@link
+ android.widget.LinearLayout <LinearLayout>}. Then add the
{@code
-android:orientation} to be "horizontal". The result looks like this:
"horizontal".
+The result looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
+ xmlns:tools="http://schemas.android.com/tools"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
@@ -116,26 +120,18 @@ android:layout_height}, are required for all views in order to specify their
Because the {@link android.widget.LinearLayout} is the root view in the layout, it should fill
the entire screen area that's
available to the app by setting the width and height to
-"fill_parent".
Note: Beginning with Android 2.2 (API level 8),
-"fill_parent" has been renamed "match_parent" to better reflect the
-behavior. The reason is that if you set a view to "fill_parent" it does not expand to
-fill the remaining space after sibling views are considered, but instead expands to
-match the size of the parent view no matter what—it will overlap any sibling
-views.
"match_parent". This value declares that the view should expand its width
+or height to match the width or height of the parent view.
For more information about layout properties, see the XML Layout guide.
+href="{@docRoot}guide/topics/ui/declaring-layout.html">Layout guide.To create a user-editable text field, add an {@link android.widget.EditText -<EditText>} element inside the {@link android.widget.LinearLayout <LinearLayout>}. The {@link -android.widget.EditText} class is a subclass of {@link android.view.View} that displays an editable -text field.
+<EditText>} element inside the {@link android.widget.LinearLayout <LinearLayout>}.Like every {@link android.view.View} object, you must define certain XML attributes to specify the {@link android.widget.EditText} object's properties. Here’s how you should declare it @@ -164,6 +160,8 @@ href="{@docRoot}reference/android/view/View.html#attr_android:id">{@code android which allows you to reference that view from other code.
The SDK tools generate the {@code R.java} each time you compile your app. You should never modify this file by hand.
+For more information, read the guide to Providing Resources.
@@ -175,17 +173,18 @@ modify this file by hand. from your app code, such as to read and manipulate the object (you'll see this in the next lesson). -The at-symbol (@) is required when you want to refer to a resource object from
-XML, followed by the resource type ({@code id} in this case), then the resource name ({@code
-edit_message}). (Other resources can use the same name as long as they are not the same
-resource type—for example, the string resource uses the same name.)
The plus-symbol (+) is needed only when you're defining a resource ID for the
-first time. It tells the SDK tools that the resource ID needs to be created. Thus, when the app is
-compiled, the SDK tools use the ID value, edit_message, to create a new identifier in
-your project's {@code gen/R.java} file that is now associated with the {@link
-android.widget.EditText} element. Once the resource ID is created, other references to the ID do not
-need the plus symbol. This is the only attribute that may need the plus-symbol. See the sidebox for
+
The at sign (@) is required when you're referring to any resource object from
+XML. It is followed by the resource type ({@code id} in this case), a slash, then the resource name
+({@code edit_message}).
The plus sign (+) before the resource type is needed only when you're defining a
+resource ID for the first time. When you compile the app,
+the SDK tools use the ID name to create a new resource ID in
+your project's {@code gen/R.java} file that refers to the {@link
+android.widget.EditText} element. Once the resource ID is declared once this way,
+other references to the ID do not
+need the plus sign. Using the plus sign is necessary only when specifying a new resource ID and not
+needed for concrete resources such as strings or layouts. See the sidebox for
more information about resource objects.
"wrap_content" value
specifies that the view should be only as big as needed to fit the contents of the view. If you
-were to instead use "fill_parent", then the {@link android.widget.EditText}
-element would fill the screen, because it'd match the size of the parent {@link
+were to instead use "match_parent", then the {@link android.widget.EditText}
+element would fill the screen, because it would match the size of the parent {@link
android.widget.LinearLayout}. For more information, see the XML Layouts guide.Note: This string resource has the same name as the element ID: +{@code edit_message}. However, references +to resources are always scoped by the resource type (such as {@code id} or {@code string}), so using +the same name does not cause collisions.
+When you need to add text in the user interface, you should always specify each string of text in -a resource file. String resources allow you to maintain a single location for all string -values, which makes it easier to find and update text. Externalizing the strings also allows you to +
When you need to add text in the user interface, you should always specify each string as +a resource. String resources allow you to manage all UI text in a single location, +which makes it easier to find and update text. Externalizing the strings also allows you to localize your app to different languages by providing alternative definitions for each -string.
+string resource.By default, your Android project includes a string resource file at
-res/values/strings.xml. Open this file, delete the existing "hello"
-string, and add one for the
-"edit_message" string used by the {@link android.widget.EditText <EditText>}
-element.
res/values/strings.xml. Open this file and delete the {@code <string>} element
+named "hello_world". Then add a new one named
+"edit_message" and set the value to "Enter a message."
-While you’re in this file, also add a string for the button you’ll soon add, called +
While you’re in this file, also add a "Send" string for the button you’ll soon add, called
"button_send".
The result for strings.xml looks like this:
For more information about using string resources to localize your app for several languages, +
For more information about using string resources to localize your app for other languages, see the Supporting Various Devices +href="{@docRoot}training/basics/supporting-devices/index.html">Supporting Different Devices class.
@@ -280,23 +284,26 @@ android.widget.Button} widgets have their widths set to"wrap_content".
This works fine for the button, but not as well for the text field, because the user might type -something longer and there's extra space left on the screen. So, it'd be nice to fill that width -using the text field. -{@link android.widget.LinearLayout} enables such a design with the weight property, which +something longer. So, it would be nice to fill the unused screen width +with the text field. You can do this inside a +{@link android.widget.LinearLayout} with the weight property, which you can specify using the {@code android:layout_weight} attribute.
-The weight value allows you to specify the amount of remaining space each view should consume, -relative to the amount consumed by sibling views, just like the ingredients in a drink recipe: "2 +
The weight value is a number that specifies the amount of remaining space each view should +consume, +relative to the amount consumed by sibling views. This works kind of like the +amount of ingredients in a drink recipe: "2 parts vodka, 1 part coffee liqueur" means two-thirds of the drink is vodka. For example, if you give -one view a weight of 2 and another one a weight of 1, the sum is 3, so the first view gets 2/3 of -the remaining space and the second view gets the rest. If you give a third view a weight of 1, -then the first view now gets 1/2 the remaining space, while the remaining two each get 1/4.
+one view a weight of 2 and another one a weight of 1, the sum is 3, so the first view fills 2/3 of +the remaining space and the second view fills the rest. If you add a third view and give it a weight +of 1, then the first view (with weight of 2) now gets 1/2 the remaining space, while the remaining +two each get 1/4.The default weight for all views is 0, so if you specify any weight value -greater than 0 to only one view, then that view fills whatever space remains after each view is -given the space it requires. So, to fill the remaining space with the {@link +greater than 0 to only one view, then that view fills whatever space remains after all views are +given the space they require. So, to fill the remaining space in your layout with the {@link android.widget.EditText} element, give it a weight of 1 and leave the button with no weight.
@@ -331,8 +338,9 @@ android.widget.LinearLayout}.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
+ xmlns:tools="http://schemas.android.com/tools"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
@@ -351,7 +359,8 @@ that the SDK tools generated when you created the project, so you can now run th
results:
from the toolbar.diff --git a/docs/html/training/basics/firstapp/creating-project.jd b/docs/html/training/basics/firstapp/creating-project.jd index 4fbfe34..97f2a5d 100644 --- a/docs/html/training/basics/firstapp/creating-project.jd +++ b/docs/html/training/basics/firstapp/creating-project.jd @@ -34,66 +34,77 @@ SDK
An Android project contains all the files that comprise the source code for your Android app. The Android SDK tools make it easy to start a new Android project with a set of -default project directories and files.
+default project directories and files.This lesson shows how to create a new project either using Eclipse (with the ADT plugin) or using the SDK tools from a command line.
Note: You should already have the Android 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.
+you're using Eclipse, you should also have the ADT plugin installed. If you don't have +these, follow the guide to Installing the Android SDK +before you start this lesson.
+ in the toolbar. (If you don’t see this button,
+then you have not installed the ADT plugin—see Installing the Eclipse Plugin.)
+
-Figure 1. The new project wizard in Eclipse.
+Figure 1. The New Android App Project wizard in Eclipse.
We recommend that you select the latest version possible. You can still build your app to -support older versions, but setting the build target to the latest version allows you to -easily optimize your app for a great user experience on the latest Android-powered devices.
-If you don't see any built targets listed, you need to install some using the Android SDK -Manager tool. See step 4 in the -installing guide.
-Click Next.
Because this version is lower than the build target selected for the app, a warning -appears, but that's alright. You simply need to be sure that you don't use any APIs that require an -API level greater than the minimum SDK -version without first using some code to verify the device's system version (you'll see this in some -other classes).
-Leave this set to the default value for this project.
-Click Finish.
+Click Next.
+You can customize an icon in several ways and the tool generates an icon for all + screen densities. Before you publish your app, you should be sure your icon meets + the specifications defined in the Iconography + design guide.
+Click Next.
+For this project, select BlankActivity and click Next.
Your Android project is now set up with some default files and you’re ready to begin @@ -104,7 +115,7 @@ building the app. Continue to the next lesson.
Create a Project with Command Line ToolsIf you're not using the Eclipse IDE with the ADT plugin, you can instead create your project -using the SDK tools in a command line:
+using the SDK tools from a command line:tools/ path.If you don't see any targets listed, you need to install some using the Android SDK -Manager tool. See step 4 in the -installing guide.
+Manager tool. See Adding Platforms + and Packages.android create project --target <target-id> --name MyFirstApp \ ---path <path-to-workspace>/MyFirstApp --activity MyFirstActivity \ ---package com.example.myapp +--path <path-to-workspace>/MyFirstApp --activity MainActivity \ +--package com.example.myfirstapp
Replace <target-id> with an id from the list of targets (from the previous step)
and replace
diff --git a/docs/html/training/basics/firstapp/index.jd b/docs/html/training/basics/firstapp/index.jd
index 43b289b..e2b9cff 100644
--- a/docs/html/training/basics/firstapp/index.jd
+++ b/docs/html/training/basics/firstapp/index.jd
@@ -27,39 +27,21 @@ next.link=creating-project.html
project and run a debuggable version of the app. You'll also learn some fundamentals of Android app
design, including how to build a simple user interface and handle user input.
Before you start this class, be sure that you have your development environment set up. You need +
Before you start this class, be sure you have your development environment set up. You need to:
If you haven't already done this setup, read Installing -the SDK. Once you've finished the setup, you're ready to begin this class.
+If you haven't already done these tasks, start by downloading the + Android SDK and following the install steps. + Once you've finished the setup, you're ready to begin this class.
-This class uses a tutorial format that incrementally builds a small Android app in order to teach +
This class uses a tutorial format that incrementally builds a small Android app that teaches you some fundamental concepts about Android development, so it's important that you follow each step.
- -If you followed the previous lesson to create an Android project, it includes a default set of "Hello World" source files that allow you to -run the app right away.
+immediately run the app.How you run your app depends on two things: whether you have a real Android-powered device and whether you’re using Eclipse. This lesson shows you how to install and run your app on a @@ -49,14 +49,16 @@ project:
AndroidManifest.xmlsrc/res/drawable-hdpi/When you build and run the default Android project, the default {@link android.app.Activity}
-class in the src/ directory starts and loads a layout file from the
-layout/ directory, which includes a "Hello World" message. Not real exciting, but it's
-important that you understand how to build and run your app before adding real functionality to
-the app.
When you build and run the default Android app, the default {@link android.app.Activity} +class starts and loads a layout file +that says "Hello World." The result is nothing exciting, but it's +important that you understand how to run your app before you start developing.
Whether you’re using Eclipse or the command line, you need to:
+If you have a real Android-powered device, here's how you can install and run your app:
To run the app from Eclipse, open one of your project's files and click
-Run from the toolbar. Eclipse installs the app on your connected device and starts
+Run
+from the toolbar. Eclipse installs the app on your connected device and starts
it.
To start adding stuff to the app, continue to the next
+ That's how you build and run your Android app on a device!
+ To start developing, continue to the next
lesson. Whether you’re using Eclipse or the command line, you need to first create an Android Virtual
-Device (AVD). An AVD is a
-device configuration for the Android emulator that allows you to model
-different device configurations. Whether you’re using Eclipse or the command line, to run your app on the emulator you need to
+first create an Android Virtual Device (AVD). An
+AVD is a device configuration for the Android emulator that allows you to model different
+devices. To run the app from Eclipse, open one of your project's files and click
-Run from the toolbar. Eclipse installs the app on your AVD and starts it. Or to run your app from the command line: To start adding stuff to the app, continue to the next
+ That's how you build and run your Android app on the emulator!
+ To start developing, continue to the next
lesson. After completing the previous lesson, you have an app that
shows an activity (a single screen) with a text field and a button. In this lesson, you’ll add some
-code to The {@code
-android:onClick} attribute’s value, Add the corresponding method inside the Open the Tip: In Eclipse, press Ctrl + Shift + O to import missing classes
(Cmd + Shift + O on Mac). Note that, in order for the system to match this method to the method name given to In order for the system to match this method to the method name given to {@code android:onClick},
the signature must be exactly as shown. Specifically, the method must: An {@link android.content.Intent} is an object that provides runtime binding between separate
components (such as two activities). The {@link android.content.Intent} represents an
-app’s "intent to do something." You can use an {@link android.content.Intent} for a wide
+app’s "intent to do something." You can use intents for a wide
variety of tasks, but most often they’re used to start another activity. Inside the {@code sendMessage()} method, create an {@link android.content.Intent} to start
-an activity called {@code DisplayMessageActvity}:Run on the Emulator
-
@@ -131,13 +133,14 @@ devices.
-
<sdk>/tools/ and execute:
-./android avd
in the toolbar.<sdk>/tools/ and execute:
+android avd
+from the toolbar. Eclipse installs the app on your AVD and starts it.
MyFirstActivity that
-starts a new activity when the user selects the Send button.MainActivity that
+starts a new activity when the user clicks the Send button.
Respond to the Send Button
@@ -64,13 +64,13 @@ attribute to the {@link android.widget.Button <Button>} element:
sendMessage, is the name of a method in your
-activity that you want to call when the user selects the button."sendMessage", is the name of a method in your
+activity that the system calls when the user clicks the button.
-MyFirstActivity class:MainActivity class and add the corresponding method:
-/** Called when the user selects the Send button */
+/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
@@ -79,7 +79,7 @@ public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
@@ -127,7 +127,7 @@ specifies the exact app component to which the intent should be given. However,
can also be implicit, in which case the {@link android.content.Intent} does not specify
the desired component, but allows any app installed on the device to respond to the intent
as long as it satisfies the meta-data specifications for the action that's specified in various
-{@link android.content.Intent} parameters. For more informations, see the class about Interacting with Other Apps.
An intent not only allows you to start another activity, but can carry a bundle of data to the +
An intent not only allows you to start another activity, but it can carry a bundle of data to the activity as well. So, use {@link android.app.Activity#findViewById findViewById()} to get the -{@link android.widget.EditText} element and add its message to the intent:
+{@link android.widget.EditText} element and add its text value to the intent:Intent intent = new Intent(this, DisplayMessageActivity.class); @@ -148,37 +148,36 @@ intent.putExtra(EXTRA_MESSAGE, message);
An {@link android.content.Intent} can carry a collection of various data types as key-value -pairs called extras. The {@link android.content.Intent#putExtra putExtra()} method takes a -string as the key and the value in the second parameter.
+pairs called extras. The {@link android.content.Intent#putExtra putExtra()} method takes the +key name in the first parameter and the value in the second parameter. -In order for the next activity to query the extra data, you should define your keys using a +
In order for the next activity to query the extra data, you should define your key using a public constant. So add the {@code EXTRA_MESSAGE} definition to the top of the {@code -MyFirstActivity} class:
+MainActivity} class:
-public class MyFirstActivity extends Activity {
- public final static String EXTRA_MESSAGE = "com.example.myapp.MESSAGE";
+public class MainActivity extends Activity {
+ public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
...
}
-It's generally a good practice to define keys for extras with your app's package name as a prefix -to ensure it's unique, in case your app interacts with other apps.
+It's generally a good practice to define keys for intent extras using your app's package name +as a prefix. This ensures they are unique, in case your app interacts with other apps.
To start an activity, you simply need to call {@link android.app.Activity#startActivity -startActivity()} and pass it your {@link android.content.Intent}.
- -The system receives this call and starts an instance of the {@link android.app.Activity} +startActivity()} and pass it your {@link android.content.Intent}. The system receives this call +and starts an instance of the {@link android.app.Activity} specified by the {@link android.content.Intent}.
-With this method included, the complete {@code sendMessage()} method that's invoked by the Send +
With this new code, the complete {@code sendMessage()} method that's invoked by the Send button now looks like this:
-/** Called when the user selects the Send button */
+/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
@@ -195,20 +194,48 @@ work.
Create the Second Activity
-In your project, create a new class file under the src/<package-name>/
-directory called DisplayMessageActivity.java.
+
+
+Figure 1. The new activity wizard in Eclipse.
+
-Tip: In Eclipse, right-click the package name under the
-src/ directory and select New > Class.
-Enter "DisplayMessageActivity" for the name and {@code android.app.Activity} for the superclass.
+To create a new activity using Eclipse:
-Inside the class, add the {@link android.app.Activity#onCreate onCreate()} callback method:
+
+ - Click New
in the toolbar.
+ - In the window that appears, open the Android folder
+ and select Android Activity. Click Next.
+ - Select BlankActivity and click Next.
+ - Fill in the activity details:
+
+ - Project: MyFirstApp
+ - Activity Name: DisplayMessageActivity
+ - Layout Name: activity_display_message
+ - Navigation Type: None
+ - Hierarchial Parent: com.example.myfirstapp.MainActivity
+ - Title: My Message
+
+ Click Finish.
+
+
+
+If you're using a different IDE or the command line tools, create a new file named
+{@code DisplayMessageActivity.java} in the project's src/ directory, next to
+the original {@code MainActivity.java} file.
+
+Open the {@code DisplayMessageActivity.java} file. If you used Eclipse to create it, the class
+already includes an implementation of the required {@link android.app.Activity#onCreate onCreate()}
+method. There's also an implemtation of the {@link android.app.Activity#onCreateOptionsMenu
+onCreateOptionsMenu()} method, but
+you won't need it for this app so you can remove it. The class should look like this:
public class DisplayMessageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_display_message);
}
}
@@ -216,7 +243,7 @@ public class DisplayMessageActivity extends Activity {
All subclasses of {@link android.app.Activity} must implement the {@link
android.app.Activity#onCreate onCreate()} method. The system calls this when creating a new
instance of the activity. It is where you must define the activity layout and where you should
-initialize essential activity components.
+perform initial setup for the activity components.
@@ -226,22 +253,39 @@ initialize essential activity components.
{@code <activity>} element.
-Because {@code DisplayMessageActivity} is invoked using an explicit intent, it does not require
-any intent filters (such as those you can see in the manifest for MyFirstActivity). So
-the declaration for DisplayMessageActivity can be simply one line of code inside the {@code <application>}
-element:
+When you use the Eclipse tools to create the activity, it creates a default entry. It should
+look like this:
<application ... >
- <activity android:name="com.example.myapp.DisplayMessageActivity" />
...
+ <activity
+ android:name=".DisplayMessageActivity"
+ android:label="@string/title_activity_display_message" >
+ <meta-data
+ android:name="android.support.PARENT_ACTIVITY"
+ android:value="com.example.myfirstapp.MainActivity" />
+ </activity>
</application>
+The {@code
+ <meta-data>} element declares the name of this activity's parent activity
+ within the app's logical hierarchy. The Android Support Library uses this information
+ to implement default navigation behaviors, such as Up navigation.
+
+Note: During installation, you should have downloaded
+the latest Support Library. Eclipse automatically includes this library in your app project (you
+can see the library's JAR file listed under Android Dependencies). If you're not using
+Eclipse, you may need to manually add the library to your project—follow this guide for setting up the Support Library.
+
The app is now runnable because the {@link android.content.Intent} in the
first activity now resolves to the {@code DisplayMessageActivity} class. If you run the app now,
-pressing the Send button starts the
+clicking the Send button starts the
second activity, but it doesn't show anything yet.
@@ -249,15 +293,15 @@ second activity, but it doesn't show anything yet.
Every {@link android.app.Activity} is invoked by an {@link android.content.Intent}, regardless of
how the user navigated there. You can get the {@link android.content.Intent} that started your
-activity by calling {@link android.app.Activity#getIntent()} and the retrieve data contained
+activity by calling {@link android.app.Activity#getIntent()} and retrieve the data contained
within it.
In the {@code DisplayMessageActivity} class’s {@link android.app.Activity#onCreate onCreate()}
-method, get the intent and extract the message delivered by {@code MyFirstActivity}:
+method, get the intent and extract the message delivered by {@code MainActivity}:
Intent intent = getIntent();
-String message = intent.getStringExtra(MyFirstActivity.EXTRA_MESSAGE);
+String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
@@ -279,22 +323,23 @@ public void onCreate(Bundle savedInstanceState) {
// Get the message from the intent
Intent intent = getIntent();
- String message = intent.getStringExtra(MyFirstActivity.EXTRA_MESSAGE);
+ String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
+ // Set the text view as the activity layout
setContentView(textView);
}
-You can now run the app, type a message in the text field, press Send, and view the message on -the second activity.
+You can now run the app. When it opens, type a message in the text field, click Send, + and the message appears on the second activity.
-Figure 1. Both activities in the final app, running +
Figure 2. Both activities in the final app, running on Android 4.0.
That's it, you've built your first Android app!
-- cgit v1.1