docs: updates to Buildilng Your First App doc

This commit includes the following updates:
- replaces Basic activity with Empty activity
- uses default values wherever possible
- removes instructions for floating button
- removes SDK instructions. Users must use Android Studio
- updates the SDK version to API 15
- combines some instructions that were too text heavy

Bug: 29766869
Bug: 29768686
Bug: 19154557

Change-Id: Id819cb2dde74b68b5145dfefe7a8502892bb377a
This commit is contained in:
Mark Lu
2016-07-15 13:36:55 -07:00
parent f97faada5d
commit 08d1bff71f
5 changed files with 293 additions and 699 deletions

View File

@@ -71,38 +71,31 @@ android.view.View} objects.</p>
<h2 id="LinearLayout">Create a Linear Layout</h2>
<ol>
<li>In Android Studio, from the <code>res/layout</code> directory, open the {@code content_my.xml}
file.
<p>The BlankActivity template you chose when you created this project includes the
<code>content_my.xml</code> file with a {@link android.widget.RelativeLayout} root view and a
{@link android.widget.TextView} child view.</p>
</li>
<li>In the <strong>Preview</strong> pane, click the Hide icon <img src="{@docRoot}images/tools/as-hide-side.png"
style="vertical-align:baseline;margin:0; max-height:1.5em" /> to close the Preview pane.
<p> In Android Studio, when you open a layout file, youre first shown
the Preview pane. Clicking elements in this pane opens the WYSIWYG tools in the Design pane. For
this lesson, youre going to work directly with the XML.</p></li>
<li>Delete the {@link android.widget.TextView &lt;TextView>} element.</li>
<li>Change the {@link android.widget.RelativeLayout &lt;RelativeLayout>} element to
{@link android.widget.LinearLayout &lt;LinearLayout>}.</li>
<li>Add the <a href="{@docRoot}reference/android/widget/LinearLayout.html#attr_android:orientation">
{@code android:orientation}</a> attribute and set it to <code>"horizontal"</code>.</li>
<li>Remove the {@code android:padding} attributes and the {@code tools:context} attribute.
</ol>
<p>The result looks like this:</p>
<pre>
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<li>From the <code>res/layout/</code> directory, open the
<code>activity_main.xml</code> file.
<p>This XML file defines the layout of your activity. It contains the
default "Hello World" text view.</p>
</li>
<li>When you open a layout file, youre first shown the design editor in the
<a href="/studio/write/layout-editor.html">Layout Editor</a>. For this lesson,
you work directly with the XML, so click the <b>Text</b> tab to switch to
the text editor.
</li>
<li>Replace the contents of the file with the following XML:
<pre>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_my"&gt;
android:orientation="horizontal"&gt;
&lt;/LinearLayout&gt;
</pre>
</li>
</ol>
<p>{@link android.widget.LinearLayout} is a view group (a subclass of {@link
android.view.ViewGroup}) that lays out child views in either a vertical or horizontal orientation,
as specified by the <a
@@ -128,29 +121,25 @@ href="{@docRoot}guide/topics/ui/declaring-layout.html">Layout</a> guide.</p>
<h2 id="TextInput">Add a Text Field</h2>
<p>As with every {@link android.view.View} object, you must define certain XML attributes to specify
the {@link android.widget.EditText} object's properties.</p>
<p>In the <code>activity_main.xml</code> file, within the
{@link android.widget.LinearLayout &lt;LinearLayout>} element, add the following
{@link android.widget.EditText &lt;EditText>} element:</p>
<ol>
<li>In the <code>content_my.xml</code> file, within the
{@link android.widget.LinearLayout &lt;LinearLayout>} element, define an
{@link android.widget.EditText &lt;EditText>} element with the <code>id</code> attribute
set to <code>@+id/edit_message</code>.</li>
<li>Define the <code>layout_width</code> and <code>layout_height</code> attributes as
<code>wrap_content</code>.</li>
<li>Define a <code>hint</code> attribute as a string object named <code>edit_message</code>.</li>
</ol>
<p>The {@link android.widget.EditText &lt;EditText>} element should read as follows:</p>
<pre>
&lt;EditText android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<pre>&lt;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"&gt;
<b>&lt;EditText android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message" /></b>
&lt;/LinearLayout&gt;
</pre>
<p>Here are the {@link android.widget.EditText &lt;EditText>} attributes you added:</p>
<p>Here is a description of the attributes in the
{@link android.widget.EditText &lt;EditText>} you added:</p>
<dl>
<dt><a href="{@docRoot}reference/android/view/View.html#attr_android:id">{@code android:id}</a></dt>
@@ -222,29 +211,20 @@ the same name does not cause collisions.</p>
<h2 id="Strings">Add String Resources</h2>
<p>By default, your Android project includes a string resource file at
<code>res/values/strings.xml</code>. Here, you'll add a new string named
<code>"edit_message"</code> and set the value to "Enter a message."</p>
<code>res/values/strings.xml</code>. Here, you'll add two new strings.</p>
<ol>
<li>In Android Studio, from the <code>res/values</code> directory, open <code>strings.xml</code>.</li>
<li>Add a line for a string named <code>"edit_message"</code> with the value, "Enter a message".
</li>
<li>Add a line for a string named <code>"button_send"</code> with the value, "Send".
<p>You'll create the button that uses this string in the next section.</p>
</li>
</ol>
<p>The result for <code>strings.xml</code> looks like this:</p>
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
<li>From the <code>res/values/</code> directory, open <code>strings.xml</code>.</li>
<li>Add two strings so that your file looks like this:
<pre>&lt;?xml version="1.0" encoding="utf-8"?>
&lt;resources>
&lt;string name="app_name">My First App&lt;/string>
&lt;string name="edit_message">Enter a message&lt;/string>
&lt;string name="button_send">Send&lt;/string>
&lt;string name="action_settings">Settings&lt;/string>
<b>&lt;string name="edit_message">Enter a message&lt;/string>
&lt;string name="button_send">Send&lt;/string></b>
&lt;/resources>
</pre>
</li>
</ol>
<p>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,
@@ -260,40 +240,22 @@ class.</p>
<h2 id="Button">Add a Button</h2>
<ol>
<li>In Android Studio, from the <code>res/layout</code> directory, edit the <code>content_my.xml</code>
file.</li>
<li>Within the
{@link android.widget.LinearLayout &lt;LinearLayout>} element, define a
{@link android.widget.Button &lt;Button>} element immediately following the
{@link android.widget.EditText &lt;EditText>} element.</li>
<li>Set the button's width and height attributes to <code>"wrap_content"</code> so
the button is only as big as necessary to fit the button's text label.</li>
<li>Define the button's text label with the <a
href="{@docRoot}reference/android/widget/TextView.html#attr_android:text">{@code
android:text}</a> attribute; set its value to the <code>button_send</code> string
resource you defined in the previous section.</li>
</ol>
<p>Your {@link android.widget.LinearLayout &lt;LinearLayout>} should look like this:</p>
<pre>
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<p>Go back to the <code>activity_main.xml</code> file and add a button after the
{@link android.widget.EditText &lt;EditText>}. Your file should look like this:</p>
<pre>&lt;LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_my"&gt;
android:layout_height="match_parent"&gt;
&lt;EditText android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message" /&gt;
&lt;Button
<b>&lt;Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" /&gt;
android:text="@string/button_send" /&gt;</b>
&lt;/LinearLayout&gt;
</pre>
@@ -302,7 +264,7 @@ resource you defined in the previous section.</li>
attribute, because it won't be referenced from the activity code.</p>
<p>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 Figure 2 shows.
android.widget.Button} widgets are only as big as necessary to fit their content, as figure 2 shows.
</p>
<img src="{@docRoot}images/training/firstapp/edittext_wrap.png" />
@@ -334,53 +296,36 @@ given the space they require.</p>
<h2 id="Weight">Make the Input Box Fill in the Screen Width</h2>
<p>To fill the remaining space in your layout with the {@link android.widget.EditText} element, do
the following:</p>
<ol>
<li>In the <code>content_my.xml</code> file, assign the
{@link android.widget.EditText &lt;EditText>} element's <code>layout_weight</code> attribute a value
of <code>1</code>.</li>
<li>Also, assign {@link android.widget.EditText &lt;EditText>} element's <code>layout_width</code>
attribute a value of <code>0dp</code>.
<p>In <code>activity_main.xml</code>, modify the
{@link android.widget.EditText &lt;EditText>} so that the attributes look like
this:</p>
<pre>
&lt;EditText
android:layout_weight="1"
android:layout_width="0dp"
... /&gt;
&lt;EditText android:id="@+id/edit_message"
<b>android:layout_weight="1"
android:layout_width="0dp"</b>
android:layout_height="wrap_content"
android:hint="@string/edit_message" /&gt;
</pre>
<p>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
<p>Setting the width to zero (0dp) improves layout performance because using
<code>"wrap_content"</code> 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.</p>
<p>Figure 3
shows the result when you assign all weight to the {@link android.widget.EditText} element.</p>
<img src="{@docRoot}images/training/firstapp/edittext_gravity.png" />
<p class="img-caption"><strong>Figure 3.</strong> The {@link android.widget.EditText} widget is
given all the layout weight, so it fills the remaining space in the {@link
android.widget.LinearLayout}.</p>
</li>
</ol>
<p>Heres how your complete <code>activity_main.xml</code>layout file should now look:</p>
<p>Heres how your complete <code>content_my.xml</code>layout file should now look:</p>
<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
<pre>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_my"&gt;
android:layout_height="match_parent"&gt;
&lt;EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
@@ -406,7 +351,4 @@ that the SDK tools generated when you created the project.</p>
<p>Continue to the <a href="starting-activity.html">next
lesson</a> to learn how to respond to button presses, read content
from the text field, start another activity, and more.</p>
from the text field, start another activity, and more.</p>

View File

@@ -14,36 +14,19 @@ next.link=running-app.html
<div id="tb-wrapper">
<div id="tb">
<h2>This lesson teaches you to</h2>
<ol>
<li><a href="#Studio">Create a Project with Android Studio</a></li>
</ol>
<h2>You should also read</h2>
<ul>
<li><a href="{@docRoot}tools/projects/index.html">Managing Projects</a></li>
<li><a href="{@docRoot}studio/projects/index.html">Projects Overview</a></li>
</ul>
</div>
</div>
<p>An Android project contains all the files that comprise the source code for your Android
app.</p>
<p>This lesson
shows how to create a new project either using Android Studio or using the
SDK tools from a command line.</p>
<p class="note"><strong>Note:</strong> You should already have Android Studio or the Android SDK
command-line tools installed. If not, <a
href="{@docRoot}studio/index.html">download them</a> before you start this
lesson.</p>
<h2 id="Studio">Create a Project with Android Studio</h2>
<p>This lesson shows you how to create a new Android project with
<a href="{@docRoot}studio/index.html">Android Studio</a> and describes some
of the files in the project.</p>
<ol>
<li>In Android Studio, create a new project:
@@ -54,11 +37,12 @@ lesson.</p>
Project</strong>. The <em>Create New Project</em> screen appears.</li>
</ul>
</li>
<li>Fill out the fields on the screen, and click <strong>Next</strong>.
<p>It is easier to follow these lessons if you use the same values as shown.</p>
<li>Fill out the fields on the screen. For <strong>Application Name</strong>
use "My First App". For <strong>Company Domain</strong>, use "example.com".
For the other fields, use the default values and click <strong>Next</strong>
<p>Here's a brief explanation of each field:</p>
<ul>
<li><strong>Application Name</strong> is the app name that appears to users.
For this project, use "My First App."</li>
<li><strong>Application Name</strong> is the app name that appears to users.</li>
<li><strong>Company domain</strong> provides a qualifier that will be appended to the package
name; Android Studio will remember this qualifier for each new project you create.</li>
<li><strong>Package name</strong> is the fully qualified name for the project (following the
@@ -70,9 +54,8 @@ lesson.</p>
files.</li>
</ul>
</li>
<li>Under <strong>Select the form factors your app will run on</strong>, check the box for <strong>
Phone and Tablet</strong>.</li>
<li>For <strong>Minimum SDK</strong>, select <strong>API 8: Android 2.2 (Froyo)</strong>.
<li>Under <strong>Target Android Devices</strong>, accept the default values
and click <strong>Next</strong>.
<p>The Minimum Required SDK is the earliest version of Android that your app supports,
indicated using the <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#ApiLevels">
API level</a>. To support as many devices as possible, you should set this to the lowest
@@ -80,8 +63,13 @@ lesson.</p>
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 <a href="{@docRoot}training/basics/supporting-devices/platforms.html">
Supporting Different Platform Versions</a>).</p></li>
<li>Leave all of the other options (TV, Wear, and Glass) unchecked and click <strong>Next.</strong></li>
Supporting Different Platform Versions</a>).</p>
</li>
<li>Under <strong>Add an Activity to Mobile</strong>, select <strong>Empty
Activity</strong> and click <strong>Next</strong>.
</li>
<div class="sidebox-wrapper">
<div class="sidebox">
<h3>Activities</h3>
@@ -93,43 +81,25 @@ lesson.</p>
Activities</a> for more information.</p>
</div>
</div>
<li>Under <strong>Add an activity to &lt;<em>template</em>&gt;</strong>,
select <strong>Basic Activity</strong> and click <strong>Next</strong>.
</li>
<li>Under <strong>Customize the Activity</strong>, change the
<strong>Activity Name</strong> to <em>MyActivity</em>. The <strong>Layout
Name</strong> changes to <em>activity_my</em>, and the <strong>Title</strong>
to <em>MyActivity</em>. The <strong>Menu Resource Name</strong> is
<em>menu_my</em>.
</li>
<li>Click the <strong>Finish</strong> button to create the project.
</li>
<li>Under <strong>Customize the Activity</strong>, accept the default values
and click <strong>Finish</strong>.
</ol>
<p>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:</p>
<dl>
<dt><code>app/src/main/res/layout/activity_my.xml</code></dt>
<dd>This XML layout file is for the activity you added when you created the project
with Android Studio. Following the New Project workflow, Android Studio presents this file
with both a text
view and a preview of the screen UI. The file contains some default interface elements
from the material design library, including the
<a href="{@docRoot}training/appbar/index.html">app bar</a> and a floating action button.
It also includes a separate layout file with the main content.</dd>
<dt><code>app/src/main/java/com.example.myfirstapp/MainActivity.java</code></dt>
<dd>This file appears in Android Studio after the New Project wizard finishes.
It contains the class definition for the activity you created earlier. When you build
and run the app, the {@link android.app.Activity} starts and loads the
layout file that says "Hello World!"</dd>
<dt><code>app/src/main/res/layout/content_my.xml</code></dt>
<dd>This XML layout file resides in {@code activity_my.xml}, and contains some settings and
a {@code TextView} element that displays the message, "Hello world!".</dd>
<dt><code>app/src/main/res/layout/activity_main.xml</code></dt>
<dd>This XML file defines the layout of the activity. It contains a {@code TextView}
element with the text "Hello world!".</dd>
<dt><code>app/src/main/java/com.mycompany.myfirstapp/MyActivity.java</code></dt>
<dd>A tab for this file appears in Android Studio when the New Project workflow finishes. When you
select the file you see the class definition for the activity you created. When you build and
run the app, the {@link android.app.Activity} class starts the activity and loads the layout file
that says "Hello World!"</dd>
<dt><code>app/src/main/AndroidManifest.xml</code></dt>
<dd>The <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">manifest file</a> describes
the fundamental characteristics of the app and defines each of its components. You'll revisit
@@ -143,15 +113,15 @@ moment to review the most important of these:</p>
<ul>
<li><code>compiledSdkVersion</code> 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 <a href="{@docRoot}studio/intro/update.html">SDK Manager</a>.)
By default, this is set to the latest version of Android SDK installed on your
development machine.
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.</li>
<li><code>applicationId</code> is the fully qualified package name for your application that
you specified during the New Project workflow.</li>
you specified in the New Project wizard.</li>
<li><code>minSdkVersion</code> 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.</li>
wizard. This is the earliest version of the Android SDK that your app supports.</li>
<li><code>targetSdkVersion</code> 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
@@ -172,8 +142,8 @@ moment to review the most important of these:</p>
for various <a href="{@docRoot}training/multiscreen/screendensities.html">densities</a>.
</dd>
<dt><code>layout/</code></dt>
<dd>Directory for files that define your app's user interface like {@code activity_my.xml},
discussed above, which describes a basic layout for the {@code MyActivity}
<dd>Directory for files that define your app's user interface like {@code activity_main.xml},
discussed above, which describes a basic layout for the {@code MainActivity}
class.</dd>
<dt><code>menu/</code></dt>
<dd>Directory for files that define your app's menu items.</dd>

View File

@@ -23,25 +23,10 @@ helpoutsWidget=true
<p>Welcome to Android application development!</p>
<p>This class teaches you how to build your first Android app. Youll learn how to create an Android
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.</p>
<p>This class teaches you how to build your first Android app. Youll learn how
to create an Android project with Android Studio 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.</p>
<h2>Set Up Your Environment</h2>
<p>Before you start this class, be sure you have your development environment set up. You need
to:</p>
<ol>
<li>Download <a href="{@docRoot}studio/index.html">Android Studio</a>.</li>
<li>Download the latest SDK tools and platforms using the
<a href="{@docRoot}tools/help/sdk-manager.html">SDK Manager</a>.</li>
</ol>
<p class="note"><strong>Note:</strong> Although most of this training class
expects that you're using Android Studio, some procedures include alternative
instructions for using
the SDK tools from the command line instead.</p>
<p>This class uses a tutorial format to create a small Android app that teaches
you some fundamental concepts about Android development, so it's important that you follow each
step.</p>
<p>Before you start this class, download and install
<a href="{@docRoot}studio/index.html">Android Studio</a>.</p>

View File

@@ -26,7 +26,6 @@ helpoutsWidget=true
<ul>
<li><a href="{@docRoot}tools/device.html">Using Hardware Devices</a></li>
<li><a href="{@docRoot}tools/devices/managing-avds.html">Managing AVDs with AVD Manager</a></li>
<li><a href="{@docRoot}tools/projects/index.html">Managing Projects</a></li>
</ul>
@@ -34,27 +33,20 @@ helpoutsWidget=true
</div>
<p>If you followed the <a href="creating-project.html">previous lesson</a> to create an
Android project, it includes a default set of "Hello World" source files that allow you to
immediately run the app.</p>
<p>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.</p>
<p>In the <a href="creating-project.html">previous lesson</a>, you created an
Android project. The project contains a default app that displays
"Hello World". In this lesson, you will run the app on a device or emulator.</p>
<h2 id="RealDevice">Run on a Real Device</h2>
<p>If you have a device running Android, here's how to install and run your app.</p>
<h3>Set up your device</h3>
<p>Set up your device as follows:</p>
<ol>
<li>Plug in your device to your development machine with a USB cable.
<li>Connect your device to your development machine with a USB cable.
If you're developing on Windows, you might need to install the appropriate USB driver for your
device. For help installing drivers, see the <a href="{@docRoot}tools/extras/oem-usb.html">OEM
device. For help installing drivers, see the <a href="{@docRoot}studio/run/oem-usb.html">OEM
USB Drivers</a> document.</li>
<li>Enable <strong>USB debugging</strong> on your device. On Android 4.0 and newer, go to
<li>Enable <strong>USB debugging</strong> on your device by going to
<strong>Settings > Developer options</strong>.
<p class="note"><strong>Note:</strong> On Android 4.2 and newer, <strong>Developer
options</strong> is hidden by default. To make it available, go
@@ -63,73 +55,61 @@ line tools.</p>
</li>
</ol>
<h3>Run the app from Android Studio</h3>
<p>Run the app from Android Studio as follows:</p>
<ol>
<li>Select one of your project's files and click
<strong>Run</strong> <img
src="{@docRoot}images/tools/as-run.png" style="vertical-align:baseline;margin:0; max-height:1em" />
from the toolbar.</li>
<li>In the <strong>Choose Device</strong> window that appears, select the
<strong>Choose a running device</strong> radio button, select your device, and click <strong>OK
</strong>.</li>
<li>In Android Studio, select your project and click
<strong>Run</strong> <img src="{@docRoot}images/tools/as-run.png"
style="vertical-align:baseline;margin:0; max-height:1em" />
from the toolbar.</li>
<li>In the <strong>Select Deployment Target</strong> window,
select your device, and click <strong>OK</strong>.</li>
</ol>
<p>Android Studio installs the app on your connected device and starts it.</p>
<h2 id="Emulator">Run on the Emulator</h2>
<p>Whether you're using Android Studio or the command line, to run your app on the emulator you need
to first create an <a href="{@docRoot}tools/devices/index.html">Android Virtual Device</a> (AVD). An
AVD is a device configuration for the Android emulator that allows you to model a specific
device.</p>
<p>Before you run your app on an emulator, you need to create an
<a href="{@docRoot}tools/devices/index.html">Android Virtual Device</a> (AVD)
definition. An AVD definition defines the characteristics of an Android phone,
tablet, Android Wear, or Android TV device that you want to simulate in the
Android Emulator.</p>
<h3>Create an AVD</h3>
<p>Create an AVD Definition as follows:</p>
<ol>
<li>Launch the Android Virtual Device Manager:
<ul>
<li>In Android Studio, select <strong>Tools &gt; Android &gt; AVD Manager</strong>, or click
the AVD Manager icon <img src="{@docRoot}images/tools/avd-manager-studio.png"
style="vertical-align:bottom;margin:0;height:19px"> in the toolbar. The
<em>AVD Manager</em> screen appears.</li>
<li>Or, from the command line, change directories to
<code>sdk/</code> and execute:
<pre class="no-pretty-print">tools/android avd</pre>
<p class="note"><strong>Note:</strong> The AVD Manager that appears
when launched from the command line is different from the version in
Android Studio, so the following instructions may not all apply.</p>
</li>
</ul>
</li>
<li>Launch the Android Virtual Device Manager by selecting
<strong>Tools &gt; Android &gt; AVD Manager</strong>, or by clicking
the AVD Manager icon <img src="{@docRoot}images/tools/avd-manager-studio.png"
style="vertical-align:bottom;margin:0;height:19px"> in the toolbar.</li>
<li>On the AVD Manager main screen, click <strong>Create Virtual Device</strong>.</li>
<li>In the Select Hardware window, select a device configuration, such as Nexus 6,
then click <strong>Next</strong>.
<li>In the Select Hardware page, select a phone device, such as Nexus 6,
then click <strong>Next</strong>.
</li>
<li>Select the desired system version for the AVD and click <strong>Next</strong>.
<li>In the Select Image page, choose the desired system image for the AVD and
click <strong>Next</strong>.
</li>
<li>Verify the configuration settings, then click <strong>Finish</strong>.
<li>Verify the configuration settings (for your first AVD, leave all the
settings as they are), and then click <strong>Finish</strong>.
</li>
</ol>
<p>For more information about using AVDs, see
<a href="{@docRoot}tools/devices/managing-avds.html">Managing AVDs with AVD Manager</a>.</p>
<a href="{@docRoot}studio/run/managing-avds.html">Create and Manage Virtual Devices</a>.</p>
<h3>Run the app from Android Studio</h3>
<p>Run the app from Android Studio as follows:</p>
<ol>
<li>In <strong>Android Studio</strong>, select your project and click <strong>Run</strong>
<img src="{@docRoot}images/tools/as-run.png" style="vertical-align:baseline;margin:0; max-height:1em" /> from the toolbar.</li>
<li>In the <strong>Choose Device</strong> window, click the <strong>Launch emulator</strong> radio
button.</li>
<li>From the <strong>Android virtual device</strong> pull-down menu, select the emulator
you created, and click <strong>OK</strong>.</li>
<li>In <strong>Android Studio</strong>, select your project and click
<strong>Run</strong> <img src="{@docRoot}images/tools/as-run.png"
style="vertical-align:baseline;margin:0; max-height:1em" />
from the toolbar.</li>
<li>In the <strong>Select Deployment Target</strong> window,
select your emulator and click <strong>OK</strong>.</li>
</ol>
<p>It can take a few minutes for the emulator to load itself. You may have to unlock the screen.
<p>It can take a few minutes for the emulator to start. You may have to unlock the screen.
When you do, <em>My First App</em> appears on the emulator screen.</p>
<p>That's how you build and run your Android app on the emulator!
To start developing, continue to the <a href="building-ui.html">next
lesson</a>.</p>
lesson</a>.</p>

View File

@@ -19,9 +19,7 @@ helpoutsWidget=true
<ol>
<li><a href="#RespondToButton">Respond to the Send Button</a></li>
<li><a href="#BuildIntent">Build an Intent</a></li>
<!-- <li><a href="#StartActivity">Start the Second Activity</a></li> -->
<li><a href="#CreateActivity">Create the Second Activity</a></li>
<li><a href="#ReceiveIntent">Receive the Intent</a></li>
<li><a href="#DisplayMessage">Display the Message</a></li>
</ol>
@@ -33,55 +31,53 @@ helpoutsWidget=true
<p>After completing the <a href="building-ui.html">previous lesson</a>, you have an app that
shows an activity (a single screen) with a text field and a button. In this lesson, youll add some
code to <code>MyActivity</code> that
code to <code>MainActivity</code> that
starts a new activity when the user clicks the Send button.</p>
<h2 id="RespondToButton">Respond to the Send Button</h2>
<ol>
<li>In Android Studio, from the <code>res/layout</code> directory, edit the <code>content_my.xml</code>
file.</li>
<li>Add the <a
href="{@docRoot}reference/android/view/View.html#attr_android:onClick">{@code android:onClick}</a>
attribute to the {@link android.widget.Button &lt;Button&gt;} element.
<li>In the file <code>res/layout/activity_main.xml</code>, add the
<a href="{@docRoot}reference/android/view/View.html#attr_android:onClick">{@code android:onClick}</a>
attribute to the {@link android.widget.Button &lt;Button&gt;} element as
shown below:
<pre>&lt;Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
<b>android:onClick="sendMessage"</b> />
</pre>
<p>This attribute tells the system to call the <code>sendMessage()</code>
method in your activity whenever a user clicks on the button.</p>
</li>
<p class="code-caption">res/layout/content_my.xml</p>
<pre>
&lt;Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
</pre>
<li>In the file <code>java/com.example.myfirstapp/MainActivity.java</code>,
add the <code>sendMessage()</code> method stub as shown below:
<p>The <a
href="{@docRoot}reference/android/view/View.html#attr_android:onClick">{@code
android:onClick}</a> attributes value, <code>"sendMessage"</code>, is the name of a method in your
activity that the system calls when the user clicks the button.</p>
</li>
<li>In the <code>java/com.mycompany.myfirstapp</code> directory, open the <code>MyActivity.java</code> file.</li>
<li>Within the <code>MyActivity</code> class, add the {@code sendMessage()} method stub shown
below.
<pre>public class MainActivity extends AppCompatActivity {
&#64;Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
<p class="code-caption">java/com.mycompany.myfirstapp/MyActivity.java</p>
<pre>
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
</pre>
<b>/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}</b>
}</pre>
<p>In order for the system to match this method to the method name given to <a
href="{@docRoot}reference/android/view/View.html#attr_android:onClick">{@code android:onClick}</a>,
the signature must be exactly as shown. Specifically, the method must:</p>
<p>In order for the system to match this method to the method name given to <a
href="{@docRoot}reference/android/view/View.html#attr_android:onClick">{@code android:onClick}</a>,
the signature must be exactly as shown. Specifically, the method must:</p>
<ul>
<li>Be public</li>
<li>Have a void return value</li>
<li>Have a {@link android.view.View} as the only parameter (this will be the {@link
android.view.View} that was clicked)</li>
</ul>
<ul>
<li>Be public</li>
<li>Have a void return value</li>
<li>Have a {@link android.view.View} as the only parameter (this will be the {@link
android.view.View} that was clicked)</li>
</ul>
</li>
</ol>
@@ -90,422 +86,147 @@ android.view.View} that was clicked)</li>
another activity.</p>
<h2 id="BuildIntent">Build an Intent</h2>
<p>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 apps "intent to do something."
You can use intents for a wide variety of tasks, but in this lesson, your intent
starts another activity.</p>
<ol>
<li>In <code>MyActivity.java</code>, inside the {@code sendMessage()} method, create an
{@link android.content.Intent} to start an activity called {@code DisplayMessageActivity} with the
following code:
<p class="code-caption">java/com.mycompany.myfirstapp/MyActivity.java</p>
<pre>
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
}
</pre>
<div class="sidebox-wrapper">
<div class="sidebox">
<h3>Intents</h3>
<p>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
apps "intent to do something." You can use intents for a wide
variety of tasks, but most often theyre used to start another activity. For more information, see
<a href="{@docRoot}guide/components/intents-filters.html ">Intents and Intent Filters</a>.</p>
</div>
</div>
<p class="note"><strong>Note:</strong> The reference to {@code DisplayMessageActivity}
will raise an error if youre using an IDE such as Android Studio because the class doesnt exist yet.
Ignore the error for now; youll create the class soon.</p>
<p>The constructor used here takes two parameters:</p>
<ul>
<li>A {@link
android.content.Context} as its first parameter ({@code this} is used because the {@link
android.app.Activity} class is a subclass of {@link android.content.Context})
<li>The {@link java.lang.Class} of the app component to which the system should deliver
the {@link android.content.Intent} (in this case, the activity that should be started)
</ul>
<p>Android Studio indicates that you must import the {@link android.content.Intent} class.</p>
</li>
<li>At the top of the file, import the {@link android.content.Intent} class:
<p class="code-caption">java/com.mycompany.myfirstapp/MyActivity.java</p>
<pre>
import android.content.Intent;
</pre>
<p class="note"><strong>Tip:</strong> In Android Studio, press Alt + Enter (option + return on Mac)
to import missing classes.</p>
</li>
<!-- I didn't think this was necessary
<div class="sidebox-wrapper">
<div class="sidebox">
<h3>Sending an intent to other apps</h3>
<p>The intent created in this lesson is what's considered an <em>explicit intent</em>, because the
{@link android.content.Intent}
specifies the exact app component to which the intent should be given. However, intents
can also be <em>implicit</em>, 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 information, see the class about <a
href="{@docRoot}training/basics/intents/index.html">Interacting with Other Apps</a>.</p>
</div>
</div>
-->
<li>Inside the {@code sendMessage()} method,
use {@link android.app.Activity#findViewById findViewById()} to get the
{@link android.widget.EditText} element.
<p class="code-caption">java/com.mycompany.myfirstapp/MyActivity.java</p>
<pre>
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
}
</pre>
</li>
<li>At the top of the file, import the {@link android.widget.EditText} class.
<p>In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes.</p>
</li>
<li>Assign the text to a local <code>message</code> variable, and use the
{@link android.content.Intent#putExtra putExtra()} method to add its text value to the intent.
<p class="code-caption">java/com.mycompany.myfirstapp/MyActivity.java</p>
<pre>
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);
}
</pre>
<p>An {@link android.content.Intent} can carry data types as key-value
pairs called <em>extras</em>. The {@link android.content.Intent#putExtra putExtra()} method takes the
key name in the first parameter and the value in the second parameter.</p>
</li>
<li>At the top of the {@code MyActivity} class, add the {@code EXTRA_MESSAGE} definition as
follows:
<p class="code-caption">java/com.mycompany.myfirstapp/MyActivity.java</p>
<pre>
public class MyActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";
...
}
</pre>
<p>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.</p>
</li>
<!-- <h2 id="StartActivity">Start the Second Activity</h2> -->
<li>In the {@code sendMessage()} method, to finish the intent, call the
{@link android.app.Activity#startActivity startActivity()} method, passing it the
{@link android.content.Intent} object created in step 1.
</ol>
<p>With this new code, the complete {@code sendMessage()} method that's invoked by the Send
button now looks like this:</p>
<p class="code-caption">java/com.mycompany.myfirstapp/MyActivity.java</p>
<pre>
/** 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);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
</pre>
<p>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.</p>
</li>
</ol>
<h2 id="CreateActivity">Create the Second Activity</h2>
<p>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.</p>
<h3>Create a new activity using Android Studio</h3>
<p>Android Studio includes a stub for the
{@link android.app.Activity#onCreate onCreate()} method when you create a new activity. The
<em>New Android Activity</em> window appears.</p>
<ol>
<li>In Android Studio, in the <code>java</code> directory, select the package,
<strong>com.mycompany.myfirstapp</strong>, right-click, and select
<strong>New > Activity > Blank Activity</strong>.</li>
<li>In the <strong>Choose options</strong> window, fill in the activity details:
<ul>
<li><strong>Activity Name</strong>: DisplayMessageActivity</li>
<li><strong>Layout Name</strong>: activity_display_message</li>
<li><strong>Title</strong>: My Message</li>
<li><strong>Hierarchical Parent</strong>: com.mycompany.myfirstapp.MyActivity</li>
<li><strong>Package name</strong>: com.mycompany.myfirstapp</li>
</ul>
<p>Click <strong>Finish</strong>.</p>
</li>
<li>Open the {@code DisplayMessageActivity.java} file.
<p>The class already includes an implementation of the required
{@link android.app.Activity#onCreate onCreate()} method. You update the implementation of this
method later.</p>
<!-- Android Studio does not create a Fragment placeholder
<p>Also, the file includes a <code>PlaceholderFragment</code> class that extends
{@link android.app.Fragment}. This activity does not implement fragments, but you might use this
later in the training. Fragments decompose application functionality and UI into reusable modules.
For more information on fragments, see the
<a href="{@docRoot}guide/components/fragments.html">Fragments API Guide</a> and follow the training,
<a href="{@docRoot}training/basics/fragments/index.html">Building A Dynamic UI with Fragments</a>.
</p>
-->
</li>
</ol>
<!-- Not needed for Android Studio
<p class="note"><strong>Note:</strong> Your activity may look different if you did not use
the latest version of the ADT plugin. Make sure you install the latest version of the
<a href="{@docRoot}tools/sdk/eclipse-adt.html">ADT plugin</a> to complete this tutorial.</p>
-->
<p>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.</p>
<h3>Create the activity without Android Studio</h3>
<p>If you're using a different IDE or the command line tools, do the following:</p>
<ol>
<li>Create a new file named {@code DisplayMessageActivity.java} in the project's <code>src/</code>
directory, next to the original {@code MyActivity.java} file.</li>
<li>Add the following code to the file:
<pre>
public class DisplayMessageActivity extends AppCompatActivity {
<p>In <code>MainActivity.java</code>, add the code shown below to
<code>sendMessage()</code>:</p>
<pre>public class MainActivity extends AppCompatActivity {
<b>public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";</b>
&#64;Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
setContentView(R.layout.activity_main);
}
&#64;Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle app bar item clicks here. The app bar
// automatically handles clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
<b>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);
startActivity(intent);</b>
}
}</pre>
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
<p class="note"><strong>Note: </strong>Android Studio will display
<code>Cannot resolve symbol</code> errors because the code references classes
like {@link android.content.Intent} and {@link android.widget.EditText}
that have not been imported. To import these classes, you can either 1)
use Android Studio's "import class" functionality by pressing Alt + Enter
(Option + Return on Mac) or 2) manually add import statements at the top of
the file.</p>
public PlaceholderFragment() { }
<p>Theres a lot going on in <code>sendMessage()</code>, so lets explain
what's going on.</p>
&#64;Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message,
container, false);
return rootView;
}
}
}
</pre>
<p>The {@link android.content.Intent} constructor takes two parameters:</p>
<ul>
<li>A {@link android.content.Context} as its first parameter ({@code this}
is used because the {@link android.app.Activity} class is a subclass of
{@link android.content.Context})
<li>The {@link java.lang.Class} of the app component to which the system
should deliver the {@link android.content.Intent} (in this case, the
activity that should be started).
<p class="note"><strong>Note:</strong> The reference to
<code>DisplayMessageActivity</code> will raise an error in Android Studio
because the class doesnt exist yet. Ignore the error for now; youll
create the class soon.</p>
</ul>
<p class="note"><strong>Note:</strong> 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.</p>
<p>The {@link android.content.Intent#putExtra(String, String) putExtra()}
method adds the <code>EditText</code>'s value to the intent. An <code>Intent</code>
can carry data types as key-value pairs called <em>extras</em>. Your key is a
public constant <code>EXTRA_MESSAGE</code> because the next
activity uses the key to retrive the text value. It's 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.</p>
</li>
<p>The {@link android.app.Activity#startActivity(Intent) startActivity()}
method starts an instance of the <code>DisplayMessageActivity</code> specified
by the {@link android.content.Intent}. Now you need to create the class.</p>
<li>To your {@code strings.xml} file, add the new activity's title as follows:
<pre>
&lt;resources>
...
&lt;string name="title_activity_display_message">My Message&lt;/string>
&lt;/resources>
</pre>
</li>
<li>In your manifest file, <code>AndroidManifest.xml</code>, within the <code>Application</code>
element, add the
<a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code <activity>}</a> element
for your {@code DisplayMessageActivity} class, as follows:
<pre>
&lt;application ... >
...
&lt;activity
android:name="com.mycompany.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.mycompany.myfirstapp.MyActivity" >
&lt;meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mycompany.myfirstapp.MyActivity" />
&lt;/activity>
&lt;/application>
</pre>
</li>
</ol>
<p>The <a href="{@docRoot}guide/topics/manifest/activity-element.html#parent">{@code
android:parentActivityName}</a> attribute declares the name of this activity's parent activity
within the app's logical hierarchy. The system uses this value
to implement default navigation behaviors, such as <a
href="{@docRoot}design/patterns/navigation.html">Up navigation</a> on
Android 4.1 (API level 16) and higher. You can provide the same navigation behaviors for
older versions of Android by using the
<a href="{@docRoot}tools/support-library/index.html">Support Library</a> and adding
the <a href="{@docRoot}guide/topics/manifest/meta-data-element.html">{@code
<meta-data>}</a> element as shown here.</p>
<p class="note"><strong>Note:</strong> Your Android SDK should already include
the latest Android Support Library, which you installed during the
<a href="{@docRoot}studio/intro/update.html">Adding SDK Packages</a> 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 <em>Android Dependencies</em>). If you're not using
Android Studio, you need to manually add the library to your project&mdash;follow the guide for <a
href="{@docRoot}tools/support-library/setup.html">setting up the Support Library</a>
then return here.</p>
<p>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.</p>
<h2 id="ReceiveIntent">Receive the Intent</h2>
<p>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 the intent.</p>
<h2 id="CreateActivity">Create the Second Activity</h2>
<ol>
<li>In the <code>java/com.mycompany.myfirstapp</code> directory, edit the
{@code DisplayMessageActivity.java} file.</li>
<li>Get the intent and assign it to a local variable.
<pre>
Intent intent = getIntent();
</pre>
</li>
<li>At the top of the file, import the {@link android.content.Intent} class.
<p>In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes.</p>
</li>
<li>Extract the message delivered by {@code MyActivity} with the
{@link android.content.Intent#getStringExtra getStringExtra()} method.
<pre>
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
</pre>
</li>
<li>In the <b>Project</b> window, right-click the <b>app</b> folder and select
<strong>New > Activity > Empty Activity</strong>.</li>
<li>In the <strong>Configure Activity</strong> window, enter
"DisplayMessageActivity" for <strong>Activity Name</strong> and click
<strong>Finish</strong>
</li>
</ol>
<p>Android Studio automatically does three things:</p>
<ul>
<li>Creates the class <code>DisplayMessageActivity.java</code> with an
implementation of the required {@link android.app.Activity#onCreate(Bundle) onCreate()}
method.</li>
<li>Creates the corresponding layout file <code>activity_display_message.xml</code>
</li>
<li>Adds the required
<a href="{@docRoot}guide/topics/manifest/activity-element.html">&lt;activity&gt;</a>
element in <code>AndroidManifest.xml</code>.
</ul>
<p>If you run the app and click the Send button on the first activity, the
second activity starts but is empty. This is because the second activity uses
the default empty layout provided by the template.</p>
<h2 id="DisplayMessage">Display the Message</h2>
<p>Now you will modify the second activity to display the message that was passed
by the first activity.</p>
<ol>
<li>In the res/layout directory, edit the {@code content_display_message.xml} file.</li>
<li>Add an {@code android:id} attribute to the {@code RelativeLayout}.
You need this attribute to reference the object from your app code.</li>
<pre>
&lt; RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:id="@+id/content"&gt;
&lt;/RelativeLayout&gt;
</pre>
<li>Switch back to editing {@code DisplayMessageActivity.java}.</li>
<li>In the {@link android.app.Activity#onCreate onCreate()} method, create a {@link android.widget.TextView} object.
<pre>
TextView textView = new TextView(this);
</pre>
</li>
<li>Set the text size and message with {@link android.widget.TextView#setText setText()}.
<pre>
textView.setTextSize(40);
textView.setText(message);
</pre>
</li>
<li>Add the {@link android.widget.TextView} to the {@link android.widget.RelativeLayout}
identified by {@code R.id.content}.
<pre>
RelativeLayout layout = (RelativeLayout) findViewById(R.id.content);
layout.addView(textView);
</pre>
</li>
<li>At the top of the file, import the {@link android.widget.TextView} class.
<p>In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes.</p>
</li>
</ol>
<p>The complete {@link android.app.Activity#onCreate onCreate()} method for {@code
DisplayMessageActivity} now looks like this:</p>
<pre>
&#64;Override
<li>In {@code DisplayMessageActivity.java}, add the following code to the
<code>onCreate()</code> method:
<pre>&#64;Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
&#64;Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null)
.show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.content);
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
layout.addView(textView);
</pre>
}</pre>
</li>
<li>Press Alt + Enter (option + return on Mac) to import missing classes.</li>
</ol>
<p>Theres a lot going on here, so lets explain:</p>
<ol>
<li>The call {@link android.app.Activity#getIntent()} grabs the intent
that started the activity. Every {@link android.app.Activity} is invoked by an
{@link android.content.Intent}, regardless of how the user navigated there.
The call {@link android.content.Intent#getStringExtra(String) getStringExtra()}
retrieves the data from the first activity.</li>
<li>You programmatically create a {@link android.widget.TextView} and set
its size and message.
</li>
<li>You add the <code>TextView</code> to the layout identified by
{@code R.id.activity_display_message}. You cast the layout to
{@link android.view.ViewGroup} because it is the superclass of all layouts and
contains the {@link android.view.ViewGroup#addView(View) addView()}
method.</li>
</ol>
<p class="note"><strong>Note: </strong>The XML layout generated by previous
versions of Android Studio might not include the <code>android:id</code>
attribute. The call <code>findViewById()</code> will fail if the layout
does not have the <code>android:id</code> attribute. If this is the case,
open <code>activity_display_message.xml</code> and add the attribute
<code>android:id="@+id/activity_display_message"</code> to the layout element.
</p>
<p>You can now run the app. When it opens, type a message in the text field, and click
<strong>Send</strong>. The second activity replaces the first one on the screen, showing
@@ -513,8 +234,4 @@ the message you entered in the first activity.</p>
<p>That's it, you've built your first Android app!</p>
<p>To learn more, follow the link below to the next class.</p>
<p>To learn more, follow the link below to the next class.</p>