diff --git a/docs/html/images/screens_support/as-mac-avds-config.png b/docs/html/images/screens_support/as-mac-avds-config.png new file mode 100644 index 0000000000000..35467ffc99e45 Binary files /dev/null and b/docs/html/images/screens_support/as-mac-avds-config.png differ diff --git a/docs/html/images/tools/as-hide-side.png b/docs/html/images/tools/as-hide-side.png new file mode 100644 index 0000000000000..1c602f2411e92 Binary files /dev/null and b/docs/html/images/tools/as-hide-side.png differ diff --git a/docs/html/images/tools/as-run.png b/docs/html/images/tools/as-run.png new file mode 100644 index 0000000000000..76c7020db4341 Binary files /dev/null and b/docs/html/images/tools/as-run.png differ diff --git a/docs/html/images/training/firstapp/studio-new-activity.png b/docs/html/images/training/firstapp/studio-new-activity.png new file mode 100644 index 0000000000000..997d455913c05 Binary files /dev/null and b/docs/html/images/training/firstapp/studio-new-activity.png differ diff --git a/docs/html/images/training/firstapp/studio-setup-1.png b/docs/html/images/training/firstapp/studio-setup-1.png new file mode 100644 index 0000000000000..25b8fd8840535 Binary files /dev/null and b/docs/html/images/training/firstapp/studio-setup-1.png differ diff --git a/docs/html/training/basics/firstapp/building-ui.jd b/docs/html/training/basics/firstapp/building-ui.jd index 0430cdd8d0c49..dcf3a164f7e38 100644 --- a/docs/html/training/basics/firstapp/building-ui.jd +++ b/docs/html/training/basics/firstapp/building-ui.jd @@ -8,9 +8,9 @@ helpoutsWidget=true -
In this lesson, you create a layout in XML that includes a text field and a +button. In the next lesson, your app responds when the button is pressed by sending the +content of the text field to another activity.
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 buttons or -text fields and {@link -android.view.ViewGroup} objects are +text fields. +{@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.
@@ -44,6 +46,8 @@ grid or a vertical list. android.view.View} and {@link android.view.ViewGroup} so you can define your UI in XML using a hierarchy of UI elements. +Layouts are subclasses of the {@link android.view.ViewGroup}. In this exercise, you'll work with +a {@link android.widget.LinearLayout}.
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 fragment_main.xml file from the res/layout/
-directory.
Note: In Eclipse, when you open a layout file, you’re first shown -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 fragment_main.xml tab at -the bottom of the screen to open the XML editor.
- +res/layout directory, open the activity_my.xml
+file.
The BlankActivity template you chose when you created this project includes the
-fragment_main.xml file with a {@link
-android.widget.RelativeLayout} root view and a {@link android.widget.TextView} child view.
activity_my.xml file with a {@link android.widget.RelativeLayout} root view and a
+{@link android.widget.TextView} child view.
+
to close the Preview pane.
+ In Android Studio, when you open a layout file, you’re first shown + the Preview pane. Clicking elements in this pane opens the WYSIWYG tools in the Design pane. For + this lesson, you’re going to work directly with the XML.
"horizontal".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} attribute and set it to "horizontal".
-The result looks like this:
res/layout/activity_my.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
@@ -104,9 +107,9 @@ android.view.ViewGroup}) that lays out child views in either a vertical or horiz
as specified by the {@code
android:orientation} attribute. Each child of a {@link android.widget.LinearLayout} appears on
-the screen in the order in which it appears in the XML.
+the screen in the order in which it appears in the XML.
-The other two attributes, Two other attributes, {@code
android:layout_width} and {@code
@@ -122,45 +125,32 @@ or height to match the width or height of the parent view.
href="{@docRoot}guide/topics/ui/declaring-layout.html">Layout guide.
-
Add a Text Field
-To create a user-editable text field, add an {@link android.widget.EditText
-<EditText>} element inside the {@link android.widget.LinearLayout <LinearLayout>}.
+As with every {@link android.view.View} object, you must define certain XML attributes to specify
+the {@link android.widget.EditText} object's properties.
-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
-inside the {@link android.widget.LinearLayout <LinearLayout>} element:
+
+- In the
activity_my.xml file, within the
+{@link android.widget.LinearLayout <LinearLayout>} element, define an
+{@link android.widget.EditText <EditText>} element with the id attribute
+set to @+id/edit_message.
+- Define the
layout_width and layout_height attributes as
+wrap_content.
+- Define a
hint attribute as a string object named edit_message.
+
+The {@link android.widget.EditText <EditText>} element should read as follows:
+
+res/layout/activity_my.xml
- <EditText android:id="@+id/edit_message"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:hint="@string/edit_message" />
+<EditText android:id="@+id/edit_message"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:hint="@string/edit_message" />
-
-
-
- About resource objects
- A resource object is simply a unique integer name that's associated with an app resource,
-such as a bitmap, layout file, or string.
- Every resource has a
-corresponding resource object defined in your project's {@code gen/R.java} file. You can use the
-object names in the {@code R} class to refer to your resources, such as when you need to specify a
-string value for the {@code android:hint}
-attribute. You can also create arbitrary resource IDs that you associate with a view using the {@code android:id} attribute,
-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.
-
-
-
-About these attributes:
+Here are the {@link android.widget.EditText <EditText>} attributes you added:
A resource object is a unique integer name that's associated with an app resource, +such as a bitmap, layout file, or string.
+Every resource has a +corresponding resource object defined in your project's {@code gen/R.java} file. You can use the +object names in the {@code R} class to refer to your resources, such as when you need to specify a +string value for the {@code android:hint} +attribute. You can also create arbitrary resource IDs that you associate with a view using the {@code android:id} attribute, +which allows you to reference that view from other code.
+The SDK tools generate the {@code R.java} file each time you compile your app. You should never +modify this file by hand.
+For more information, read the guide to Providing Resources.
+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,
+android.widget.EditText} element. With the resource ID 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
@@ -209,26 +219,25 @@ the same name does not cause collisions.
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 resource.
-By default, your Android project includes a string resource file at
-res/values/strings.xml. Add a new string named
-"edit_message" and set the value to "Enter a message." (You can delete
-the "hello_world" string.)
res/values/strings.xml. Here, you'll add a new string named
+"edit_message" and set the value to "Enter a message."
-While you’re in this file, also add a "Send" string for the button you’ll soon add, called
-"button_send".
res/values directory, open strings.xml."edit_message" with the value, "Enter a message".
+"button_send" with the value, "Send".
+You'll create the button that uses this string in the next section.
+"hello world" string.The result for strings.xml looks like this:
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> @@ -240,35 +249,59 @@ the "hello_world" string.) </resources>+
For text in the user interface, always specify each string as +a resource. String resources allow you to manage all UI text in a single location, +which makes the text easier to find and update. Externalizing the strings also allows you to +localize your app to different languages by providing alternative definitions for each +string resource.
+For more information about using string resources to localize your app for other languages, see the Supporting Different Devices class.
- -Now add a {@link android.widget.Button <Button>} to the layout, immediately following the -{@link android.widget.EditText <EditText>} element:
+res/layout directory, edit the activity_my.xml
+file."wrap_content" so
+the button is only as big as necessary to fit the button's text label.button_send string
+resource you defined in the previous section.Your {@link android.widget.LinearLayout <LinearLayout>} should look like this:
+ +res/layout/activity_my.xml
- <Button
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ 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_width="wrap_content"
android:layout_height="wrap_content"
- android:text="@string/button_send" />
+ android:hint="@string/edit_message" />
+ <Button
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/button_send" />
+</LinearLayout>
-The height and width are set to "wrap_content" so the button is only as big as
-necessary to fit the button's text. This button doesn't need the
+
Note: This button doesn't need the {@code android:id} attribute, because it won't be referenced from the activity code.
- - -The layout is currently designed so that both the {@link android.widget.EditText} and {@link android.widget.Button} widgets are only as big as necessary to fit their content, as shown in figure 2.
@@ -279,7 +312,7 @@ 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. So, it would be nice to fill the unused screen width +something longer. 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 attribute.
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 +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 +parts soda, 1 part syrup" means two-thirds of the drink is soda. 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 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 @@ -298,38 +331,49 @@ 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 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.
+given the space they require. +To fill the remaining space in your layout with the {@link android.widget.EditText} element, do +the following:
+ +activity_my.xml file, assign the
+{@link android.widget.EditText <EditText>} element's layout_weight attribute a value
+of 1.layout_width
+attribute a value of 0dp.
+
+res/layout/activity_my.xml
- <EditText - android:layout_weight="1" - ... /> +<EditText + android:layout_weight="1" + android:layout_width="0dp" + ... />-
In order to improve the layout efficiency when you specify the weight, you should change the +
To improve the layout efficiency when you specify the weight, you should change the
width of the {@link android.widget.EditText} to be
zero (0dp). Setting the width to zero improves layout performance because using
"wrap_content" as the width requires the system to calculate a width that is
ultimately irrelevant because the weight value requires another width calculation to fill the
remaining space.
- <EditText - android:layout_weight="1" - android:layout_width="0dp" - ... /> -
Figure 3 shows the result when you assign all weight to the {@link android.widget.EditText} element.
Figure 3. The {@link android.widget.EditText} widget is -given all the layout weight, so fills the remaining space in the {@link +given all the layout weight, so it fills the remaining space in the {@link android.widget.LinearLayout}.
-Here’s how your complete layout file should now look:
+Here’s how your complete activity_my.xmllayout file should now look:
res/layout/activity_my.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" @@ -349,13 +393,16 @@ android.widget.LinearLayout}. </LinearLayout>+
This layout is applied by the default {@link android.app.Activity} class -that the SDK tools generated when you created the project, so you can now run the app to see the +that the SDK tools generated when you created the project. Run the app to see the results:
from the toolbar.
.@@ -364,7 +411,8 @@ adb install bin/MyFirstApp-debug.apk
Continue to the next lesson to learn how you can respond to button presses, read content +
Continue to the next +lesson to learn how to respond to button presses, read content from the text field, start another activity, and more.
diff --git a/docs/html/training/basics/firstapp/creating-project.jd b/docs/html/training/basics/firstapp/creating-project.jd index 2e061036cd89a..71b93c0c39056 100644 --- a/docs/html/training/basics/firstapp/creating-project.jd +++ b/docs/html/training/basics/firstapp/creating-project.jd @@ -17,7 +17,7 @@ next.link=running-app.htmlThis lesson -shows how to create a new project either using Eclipse (with the ADT plugin) or using the +shows how to create a new project either using Android Studio 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 also have the ADT -plugin installed (version 22.6.2 or higher). If you don't have these, follow the guide to +Android Studio installed. If you don't have these, follow the guide to Installing the Android SDK before you start this lesson.
-
in the toolbar.
-Figure 1. The New Android App Project wizard in Eclipse.
-
+ Figure 1. Configuring a new project in Android Studio.
+It will probably be easier to follow these lessons if you use the same values as shown.
As new versions of Android become available, you should - test your app on the new version and update this value to match the latest API level - in order to take advantage of new platform features.
-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.
-The Minimum Required SDK is the earliest version of Android that your app supports, + indicated using the + API level. To support as many devices as possible, you should set this to the lowest + version available that allows your app to provide its core feature set. If any feature of your + app is possible only on newer versions of Android and it's not critical to the app's core + feature set, you can enable the feature only when running on the versions that support it (as + discussed in + Supporting Different Platform Versions).
An activity is one of the distinguishing features of the Android framework. Activities + provide the user with access to your app, and there may be many activities. An application + will usually have a main activity for when the user launches the application, another + activity for when she selects some content to view, for example, and other activities for + when she performs other tasks within the app. See + Activities for more information.
+Your Android project is now a basic "Hello World" app that contains some default files. -To run the app, continue to the next lesson.
+Your Android project is now a basic "Hello World" app that contains some default files. Take a +moment to review the most important of these:
+app/src/main/res/layout/activity_my.xmlTextView
+ element that displays the message, "Hello world!"app/src/main/java/com.mycompany.myfirstapp/MyActivity.javaapp/src/res/AndroidManifest.xmlapp/build.gradlebuild.gradle
+ file for each module of your project, as well as a build.gradle file for the entire
+ project. Usually, you're only interested in the build.gradle file for the module,
+ in this case the app or application module. This is where your app's build dependencies
+ are set, including the defaultConfig settings:
+ compiledSdkVersion is the platform version against which you will compile
+ your app. By default, this is set to the latest version of Android available in your SDK.
+ (It should be Android 4.1 or greater; if you don't have such a version available, you must
+ install one using the SDK Manager.)
+ You can still build your app to support older versions, but setting this to the latest
+ version allows you to enable new features and optimize your app for a great user experience
+ on the latest devices.applicationId is the fully qualified package name for your application that
+ you specified during the New Project workflow.minSdkVersion is the Minimum SDK version you specified during the New Project
+ workflow. This is the earliest version of the Android SDK that your app supports.targetSdkVersion indicates the highest version of Android with which you have
+ tested your application. As new versions of Android become available, you should
+ test your app on the new version and update this value to match the latest API level and
+ thereby take advantage of new platform features. For more information, read
+ Supporting Different
+ Platform Versions.See Building Your Project with Gradle + for more information about Gradle.
Note also the /res subdirectories that contain the
+resources for your application:
drawable-hdpi/layout/values/To run the app, continue to the next lesson.
If you're not using the Eclipse IDE with the ADT plugin, you can instead create your project +
If you're not using the Android Studio IDE, you can instead create your project using the SDK tools from a command line:
android list targets
This prints a list of the available Android platforms that you’ve downloaded for your SDK. Find -the platform against which you want to compile your app. Make a note of the target id. We +the platform against which you want to compile your app. Make a note of the target ID. We recommend that you select the highest version possible. You can still build your app to support older versions, but setting the build target to the latest version allows you to optimize your app for the latest devices.
If you don't see any targets listed, you need to install some using the Android SDK -Manager tool. See Adding Platforms - and Packages.
android create project --target <target-id> --name MyFirstApp \ ---path <path-to-workspace>/MyFirstApp --activity MainActivity \ +--path <path-to-workspace>/MyFirstApp --activity MyActivity \ --package com.example.myfirstapp-
Replace <target-id> with an id from the list of targets (from the previous step)
+
Replace <target-id> with an ID from the list of targets (from the previous step)
and replace
<path-to-workspace> with the location in which you want to save your Android
projects.
Your Android project is now a basic "Hello World" app that contains some default files. -To run the app, continue to the next lesson.
-Tip: Add the platform-tools/ as well as the
tools/ directory to your PATH environment variable.
Your Android project is now a basic "Hello World" app that contains some default files. +To run the app, continue to the next lesson.
+ diff --git a/docs/html/training/basics/firstapp/index.jd b/docs/html/training/basics/firstapp/index.jd index ac8e64a726583..d5df0b7602939 100644 --- a/docs/html/training/basics/firstapp/index.jd +++ b/docs/html/training/basics/firstapp/index.jd @@ -15,9 +15,8 @@ helpoutsWidget=trueBefore you start this class, be sure you have your development environment set up. You need to:
Note: Make sure you install the most recent versions of the ADT -plugin and the Android SDK before you start this class. The procedures described in this class may +
Note: Make sure you install the most recent versions of Android +Studio and the Android SDK before you start this class. The procedures described in this class may not apply to earlier versions.
If you haven't already done these tasks, start by downloading the diff --git a/docs/html/training/basics/firstapp/running-app.jd b/docs/html/training/basics/firstapp/running-app.jd index 96b71726823be..50b4fea49aeed 100644 --- a/docs/html/training/basics/firstapp/running-app.jd +++ b/docs/html/training/basics/firstapp/running-app.jd @@ -11,9 +11,9 @@ helpoutsWidget=true -
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 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 -real device and on the Android emulator, and in both cases with either Eclipse or the command line -tools.
- -Before you run your app, you should be aware of a few directories and files in the Android -project:
- -AndroidManifest.xmlOne of the most important elements your manifest should include is the {@code <uses-sdk>} -element. This declares your app's compatibility with different Android versions using the {@code android:minSdkVersion} -and {@code android:targetSdkVersion} -attributes. For your first app, it should look like this:
--<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... > - <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> - ... -</manifest> --
You should always set the {@code android:targetSdkVersion} -as high as possible and test your app on the corresponding platform version. For more information, -read Supporting Different -Platform Versions.
- -src/res/drawable-hdpi/layout/values/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.
- - +How you run your app depends on two things: whether you have a real device running Android and +whether you're using Android Studio. This lesson shows you how to install and run your app on a +real device and on the Android emulator, and in both cases with either Android Studio or the command +line tools.
If you have a real Android-powered device, here's how you can install and run your app:
+If you have a device running Android, here's how to install and run your app.
+ +To run the app from Eclipse:
+Eclipse installs the app on your connected device and starts it.
+Android Studio installs the app on your connected device and starts it.
-Or to run your app from a command line:
+platform-tools/ directory is included in your
PATH environment variable, then execute:
adb install bin/MyFirstApp-debug.apk
That's how you build and run your Android app on a device! @@ -152,64 +100,67 @@ lesson.
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.
+Whether you're using Android Studio 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 a specific +device.
- Figure 1. The AVD Manager showing a few virtual -devices.
+
+ Figure 1. The AVD Manager showing a virtual device.
To create an AVD:
+
from the toolbar.<sdk>/tools/ and execute:
-android avd
.<sdk>/tools/ and execute:
+ android avd
When you select a device type, most of the fields auto-populate.
+It really doesn't matter what you enter here since you're not using any storage. But if you + reuse this AVD, you might have to adjust this setting.
To run the app from Eclipse:
+
+ from the toolbar.Eclipse installs the app on your AVD and starts it.
+It can take a few minutes for the emulator to load itself. You may have to unlock the screen. + When you do, My First App appears on the emulator screen.
-Or to run your app from the command line:
+ant debug
ant debug
platform-tools/ directory is included in your
-PATH environment
-variable, then execute:
-adb install bin/MyFirstApp-debug.apk
PATH environment variable, then execute:
+ adb install bin/MyFirstApp-debug.apk+
That's how you build and run your Android app on the emulator! +
That's how you build and run your Android app on the emulator! To start developing, continue to the next lesson.
diff --git a/docs/html/training/basics/firstapp/starting-activity.jd b/docs/html/training/basics/firstapp/starting-activity.jd index 71f66dd74de0f..7aad8940ccca3 100644 --- a/docs/html/training/basics/firstapp/starting-activity.jd +++ b/docs/html/training/basics/firstapp/starting-activity.jd @@ -19,7 +19,7 @@ helpoutsWidget=trueAfter 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 MainActivity that
+code to MyActivity that
starts a new activity when the user clicks the Send button.
To respond to the button's on-click event, open the fragment_main.xml
-layout file and add the
+res/layout directory, edit the activity_my.xml
+file.
res/layout/activity_my.xml
<Button
android:layout_width="wrap_content"
@@ -63,10 +66,12 @@ attribute to the {@link android.widget.Button <Button>} element:
href="{@docRoot}reference/android/view/View.html#attr_android:onClick">{@code
android:onClick} attribute’s value, "sendMessage", is the name of a method in your
activity that the system calls when the user clicks the button.
+
+java/com.mycompany.myfirstapp directory, open the MyActivity.java file.MyActivity class, add the {@code sendMessage()} method stub shown
+below.
-Open the MainActivity class (located in the project's
-src/ directory) and add the corresponding method:
java/com.mycompany.myfirstapp/MyActivity.java
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
@@ -85,32 +90,40 @@ the signature must be exactly as shown. Specifically, the method must:
android.view.View} that was clicked)Next, you’ll fill in this method to read the contents of the text field and deliver that text to another activity.
- -MyActivity.java, inside the {@code sendMessage()} method, create an
+{@link android.content.Intent} to start an activity called {@code DisplayMessageActivity} with the
+following code:
+
+java/com.mycompany.myfirstapp/MyActivity.java
+
+public void sendMessage(View view) {
+ Intent intent = new Intent(this, DisplayMessageActivity.class);
+}
+
+
+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 intents for a wide -variety of tasks, but most often they’re used to start another activity.
+variety of tasks, but most often they’re used to start another activity. For more information, see +Intents and Intent Filters. +Inside the {@code sendMessage()} method, create an {@link android.content.Intent} to start -an activity called {@code DisplayMessageActivity}:
- --Intent intent = new Intent(this, DisplayMessageActivity.class); -- -
This requires that you import the {@link android.content.Intent} class:
--import android.content.Intent; -- -
Tip: In Eclipse, press Ctrl + Shift + O to import missing classes -(Cmd + Shift + O on Mac).
+Note: The reference to {@code DisplayMessageActivity} +will raise an error if you’re using an IDE such as Android Studio because the class doesn’t exist yet. +Ignore the error for now; you’ll create the class soon.
The constructor used here takes two parameters:
Android Studio indicates that you must import the {@link android.content.Intent} class.
+ +java/com.mycompany.myfirstapp/MyActivity.java
++import android.content.Intent; ++
Tip: In Android Studio, press Alt + Enter (option + return on Mac) + to import missing classes.
+Note: The reference to {@code DisplayMessageActivity} -will raise an error if you’re using an IDE such as Eclipse because the class doesn’t exist yet. -Ignore the error for now; you’ll create the class soon.
- -An intent not only allows you to start another activity, but it can carry a bundle of data to the -activity as well. Inside the {@code sendMessage()} method, +
java/com.mycompany.myfirstapp/MyActivity.java
-Intent intent = new Intent(this, DisplayMessageActivity.class);
-EditText editText = (EditText) findViewById(R.id.edit_message);
-String message = editText.getText().toString();
-intent.putExtra(EXTRA_MESSAGE, message);
+public void sendMessage(View view) {
+ Intent intent = new Intent(this, DisplayMessageActivity.class);
+ EditText editText = (EditText) findViewById(R.id.edit_message);
+}
+
+In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes.
+message variable, and use the
+{@link android.content.Intent#putExtra putExtra()} method to add its text value to the intent.
+java/com.mycompany.myfirstapp/MyActivity.java
+
+public void sendMessage(View view) {
+ Intent intent = new Intent(this, DisplayMessageActivity.class);
+ EditText editText = (EditText) findViewById(R.id.edit_message);
+ String message = editText.getText().toString();
+ intent.putExtra(EXTRA_MESSAGE, message);
+}
-Note:
-You now need an import statement for android.widget.EditText.
-You'll define the EXTRA_MESSAGE constant in a moment.
An {@link android.content.Intent} can carry a collection of various data types as key-value +
An {@link android.content.Intent} can carry data types as key-value 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 the key -for your intent's extra using a -public constant. So add the {@code EXTRA_MESSAGE} definition to the top of the {@code -MainActivity} class:
- +java/com.mycompany.myfirstapp/MyActivity.java
-public class MainActivity extends ActionBarActivity {
- public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
+public class MyActivity extends ActionBarActivity {
+ public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";
...
}
-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.
+For the next activity to query the extra data, you should define the key +for your intent's extra using a public constant. It's generally a good practice to define keys for +intent extras using your app's package name as a prefix. This ensures the keys are unique, in case +your app interacts with other apps.
+To start an activity, 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} -specified by the {@link android.content.Intent}.
+With this new code, the complete {@code sendMessage()} method that's invoked by the Send button now looks like this:
- +java/com.mycompany.myfirstapp/MyActivity.java
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
@@ -198,70 +234,92 @@ public void sendMessage(View view) {
}
-Now you need to create the {@code DisplayMessageActivity} class in order for this to -work.
+The system receives this call and starts an instance of the {@link android.app.Activity} +specified by the {@link android.content.Intent}. Now you need to create the +{@code DisplayMessageActivity} class in order for this to work.
+ +All subclasses of {@link android.app.Activity} must implement the +{@link android.app.Activity#onCreate onCreate()} method. This method is where the activity receives +the intent with the message, then renders the message. Also, the +{@link android.app.Activity#onCreate onCreate()} method must define the activity +layout with the {@link android.app.Activity#setContentView setContentView()} method. This is where +the activity performs the initial setup of the activity components.
+ +
-Figure 1. The new activity wizard in Eclipse.
+
+Figure 1. The new activity wizard in Android Studio.
To create a new activity using Eclipse:
+Android Studio includes a stub for the +{@link android.app.Activity#onCreate onCreate()} method when you create a new activity.
in the toolbar.java directory, select the package,
+ com.mycompany.myfirstapp, right-click, and select
+ New > Activity > Blank Activity.Click Finish.
The class already includes an implementation of the required +{@link android.app.Activity#onCreate onCreate()} method. You will update the implementation of this +method later. It also includes an implementation of +{@link android.app.Activity#onOptionsItemSelected onOptionsItemSelected()}, which handles the action +bar's Up behavior. Keep these two methods as they are for now.
+ + +You won't need it for this app.
+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 this -activity:
-PlaceholderFragment class that extends
-{@link android.app.Fragment}. You will not need this class in the final version of this
-activity.Fragments decompose application functionality and UI into reusable modules. For more -information on fragments, see the Fragments -API Guide. The final version of this activity does not use fragments.
- + -The {@code DisplayMessageActivity} class should now look like this:
+If you're developing with Android Studio, you can run the app now, but not much happens. +Clicking the Send button starts the second activity, but it uses +a default "Hello world" layout provided by the template. You'll soon update the +activity to instead display a custom text view.
+ + +If you're using a different IDE or the command line tools, do the following:
+ +src/
+directory, next to the original {@code MyActivity.java} file.
public class DisplayMessageActivity extends ActionBarActivity {
@@ -307,63 +365,44 @@ public class DisplayMessageActivity extends ActionBarActivity {
}
-If you used an IDE other than Eclipse, update your {@code DisplayMessageActivity} -class with the above code.
- -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. This method is where you must define the activity layout -with the {@link android.app.Activity#setContentView setContentView()} method -and is where you should -perform initial setup for the activity components.
- -Note: If you are using an IDE other than Eclipse, your project +
Note: If you are using an IDE other than Android Studio, your project does not contain the {@code activity_display_message} layout that's requested by {@link android.app.Activity#setContentView setContentView()}. That's OK because you will update this method later and won't be using that layout.
+If you used Eclipse, you can skip to the next section, -because the template provides -the title string for the new activity.
- -If you're using an IDE other than Eclipse, -add the new activity's title to the {@code strings.xml} file:
+
<resources>
...
<string name="title_activity_display_message">My Message</string>
</resources>
+All activities must be declared in your manifest file, AndroidManifest.xml, using an
-{@code <activity>} element.
When you use the Eclipse tools to create the activity, it creates a default entry. If you're -using a different IDE, you need to add the manifest entry yourself. It should -look like this:
+AndroidManifest.xml, within the Application
+element, add the
+{@code <activity>} element
+for your {@code DisplayMessageActivity} class, as follows:
<application ... >
...
<activity
- android:name="com.example.myfirstapp.DisplayMessageActivity"
+ android:name="com.mycompany.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
- android:parentActivityName="com.example.myfirstapp.MainActivity" >
+ android:parentActivityName="com.mycompany.myfirstapp.MyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
- android:value="com.example.myfirstapp.MainActivity" />
+ android:value="com.mycompany.myfirstapp.MyActivity" />
</activity>
</application>
+The {@code android:parentActivityName} attribute declares the name of this activity's parent activity within the app's logical hierarchy. The system uses this value @@ -376,20 +415,16 @@ the {@code <meta-data>} element as shown here.
Note: Your Android SDK should already include -the latest Android Support Library. It's included with the ADT Bundle but if you're using -a different IDE, you should have installed it during the -Adding Platforms and Packages step. -When using the templates in Eclipse, the Support Library is automatically added to your app project +the latest Android Support Library, which you installed during the +Adding SDK Packages step. +When using the templates in Android Studio, the Support Library is automatically added to your app project (you can see the library's JAR file listed under Android Dependencies). If you're not using -Eclipse, you need to manually add the library to your project—follow the guide for setting up the Support Library then return here.
-If you're developing with Eclipse, you can run the app now, but not much happens. -Clicking the Send button starts the second activity but it uses -a default "Hello world" layout provided by the template. You'll soon update the -activity to instead display a custom text view, so if you're using a different IDE, -don't worry that the app won't yet compile.
+If you're using a different IDE than Android Studio, don't worry that the app won't yet compile. +You'll soon update the activity to display a custom text view.
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 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 MainActivity}:
+within the intent. +java/com.mycompany.myfirstapp directory, edit the
+ {@code DisplayMessageActivity.java} file.+ setContentView(R.layout.activity_display_message); ++
Intent intent = getIntent(); -String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);- - +
In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes.
++String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE); ++
To show the message on the screen, create a {@link android.widget.TextView} widget and set the -text using {@link android.widget.TextView#setText setText()}. Then add the {@link -android.widget.TextView} as the root view of the activity’s layout by passing it to {@link -android.app.Activity#setContentView setContentView()}.
++TextView textView = new TextView(this); ++
+textView.setTextSize(40); +textView.setText(message); ++
+setContentView(textView); ++
In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes.
+The complete {@link android.app.Activity#onCreate onCreate()} method for {@code DisplayMessageActivity} now looks like this:
@@ -426,7 +492,7 @@ public void onCreate(Bundle savedInstanceState) { // Get the message from the intent Intent intent = getIntent(); - String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); + String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE); // Create the text view TextView textView = new TextView(this);