Loading docs/html/training/basics/firstapp/building-ui.jd +17 −17 Original line number Original line Diff line number Diff line Loading @@ -71,17 +71,17 @@ android.view.View} objects.</p> <h2 id="LinearLayout">Create a Linear Layout</h2> <h2 id="LinearLayout">Create a Linear Layout</h2> <ol> <ol> <li>From the <code>res/layout/</code> directory, open the <li>In Android Studio's <b>Project</b> window, open <b>app > res > <code>activity_main.xml</code> file. layout > activity_main.xml</b>. <p>This XML file defines the layout of your activity. It contains the <p>This XML file defines the layout of your activity. It contains the default "Hello World" text view.</p> default "Hello World" text view.</p> </li> </li> <li>When you open a layout file, you’re first shown the design editor in the <li>When you open a layout file, you’re first shown the design editor in the <a href="/studio/write/layout-editor.html">Layout Editor</a>. For this lesson, <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 you work directly with the XML, so click the <b>Text</b> tab at the bottom the text editor. of the window to switch to the text editor. </li> </li> <li>Replace the contents of the file with the following XML: <li>Delete everything and insert the following XML: <pre><?xml version="1.0" encoding="utf-8"?> <pre><?xml version="1.0" encoding="utf-8"?> <LinearLayout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android" Loading Loading @@ -138,6 +138,9 @@ href="{@docRoot}guide/topics/ui/declaring-layout.html">Layout</a> guide.</p> </LinearLayout> </LinearLayout> </pre> </pre> <p>Don't worry about the error that appears for <code>@string/edit_message</code>; you'll fix that soon.</p> <p>Here is a description of the attributes in the <p>Here is a description of the attributes in the {@link android.widget.EditText <EditText>} you added:</p> {@link android.widget.EditText <EditText>} you added:</p> Loading @@ -157,7 +160,7 @@ XML. It is followed by the resource type ({@code id} in this case), a slash, the <p>A resource object is a unique integer name that's associated with an app resource, <p>A resource object is a unique integer name that's associated with an app resource, such as a bitmap, layout file, or string.</p> such as a bitmap, layout file, or string.</p> <p>Every resource has a <p>Every resource has a corresponding resource object defined in your project's {@code gen/R.java} file. You can use the corresponding resource object defined in your project's {@code 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 object names in the {@code R} class to refer to your resources, such as when you need to specify a string value for the <a string value for the <a href="{@docRoot}reference/android/widget/TextView.html#attr_android:hint">{@code android:hint}</a> href="{@docRoot}reference/android/widget/TextView.html#attr_android:hint">{@code android:hint}</a> Loading @@ -174,7 +177,7 @@ href="{@docRoot}guide/topics/resources/providing-resources.html">Providing Resou <p>The plus sign (<code>+</code>) before the resource type is needed only when you're defining a <p>The plus sign (<code>+</code>) before the resource type is needed only when you're defining a resource ID for the first time. When you compile the app, 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 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 your project's {@code R.java} file that refers to the {@link android.widget.EditText} element. With the resource ID declared once this way, android.widget.EditText} element. With the resource ID declared once this way, other references to the ID do not 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 need the plus sign. Using the plus sign is necessary only when specifying a new resource ID and not Loading Loading @@ -211,10 +214,10 @@ the same name does not cause collisions.</p> <h2 id="Strings">Add String Resources</h2> <h2 id="Strings">Add String Resources</h2> <p>By default, your Android project includes a string resource file at <p>By default, your Android project includes a string resource file at <code>res/values/strings.xml</code>. Here, you'll add two new strings.</p> <b>res > values > strings.xml</b>. Here, you'll add two new strings.</p> <ol> <ol> <li>From the <code>res/values/</code> directory, open <code>strings.xml</code>.</li> <li>From the <b>Project</b> window, open <b>res > values > strings.xml</b>.</li> <li>Add two strings so that your file looks like this: <li>Add two strings so that your file looks like this: <pre><?xml version="1.0" encoding="utf-8"?> <pre><?xml version="1.0" encoding="utf-8"?> <resources> <resources> Loading Loading @@ -340,15 +343,12 @@ android.widget.LinearLayout}.</p> <h2>Run Your App</h2> <h2>Run Your App</h2> <p>This layout is applied by the default {@link android.app.Activity} class <p>To see how the app now looks on your device or emulator, that the SDK tools generated when you created the project.</p> click <strong>Run</strong> <p>To run the app and see the results, click <strong>Run 'app'</strong> <img src="{@docRoot}images/tools/as-run.png" <img src="{@docRoot}images/tools/as-run.png" style="vertical-align:baseline;margin:0; max-height:1em" /> in the style="vertical-align:baseline;margin:0; max-height:1em" /> in the toolbar.</p> toolbar.</p> <p>Continue to the <a href="starting-activity.html">next <p>To add app behaviors such as responding to a button and starting lesson</a> to learn how to respond to button presses, read content another activity, continue to the <a href="starting-activity.html">next from the text field, start another activity, and more.</p> lesson</a>.</p> No newline at end of file No newline at end of file docs/html/training/basics/firstapp/creating-project.jd +34 −92 Original line number Original line Diff line number Diff line Loading @@ -31,129 +31,71 @@ next.link=running-app.html <ol> <ol> <li>In Android Studio, create a new project: <li>In Android Studio, create a new project: <ul> <ul> <li>If you don't have a project opened, in the <strong>Welcome</strong> screen, click <strong> <li>If you don't have a project opened, in the <strong>Welcome to Android Studio</strong> window, click <strong> New Project</strong>.</li> Start a new Android Studio project</strong>.</li> <li>If you have a project opened, from the <strong>File</strong> menu, select <strong>New <li>If you have a project opened, select <strong>File > New Project</strong>.</li> Project</strong>. The <em>Create New Project</em> screen appears.</li> </ul> </ul> </li> </li> <li>Fill out the fields on the screen. For <strong>Application Name</strong> <li>In the <b>New Project</b> screen, enter the following values:</p> 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> <ul> <li><strong>Application Name</strong> is the app name that appears to users.</li> <li><strong>Application Name</strong>: "My First App" </li> <li><strong>Company domain</strong> provides a qualifier that will be appended to the package <li><strong>Company Domain</strong>: "example.com"</li> 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 same rules as those for naming packages in the Java programming language). Your package name must be unique across all packages installed on the Android system. You can <strong> Edit</strong> this value independently from the application name or the company domain.</li> <li><strong>Project location</strong> is the directory on your system that holds the project files.</li> </ul> </ul> <p>Android Studio fills in the package name and project location for you, but you can edit these if you'd like. </li> </li> <li>Under <strong>Target Android Devices</strong>, accept the default values <li>Click <b>Next</b>.</li> and click <strong>Next</strong>. <li>In the <b>Target Android Devices</b> screen, keep the default values and <p>The Minimum Required SDK is the earliest version of Android that your app supports, click <b>Next</b>. indicated using the <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#ApiLevels"> <p>The <b>Minimum Required SDK</b> is the earliest version of Android that your app supports, which is indicated by 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 API level</a>. 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 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 app is possible only on newer versions of Android and it's not critical to the core feature set, you can enable the feature only when running on the versions that support it (as feature set, enable that feature only when running on the versions that support it (see discussed in <a href="{@docRoot}training/basics/supporting-devices/platforms.html"> <a href="{@docRoot}training/basics/supporting-devices/platforms.html"> Supporting Different Platform Versions</a>).</p> Supporting Different Platform Versions</a>).</p> </li> </li> <li>Under <strong>Add an Activity to Mobile</strong>, select <strong>Empty <li>In the <strong>Add an Activity to Mobile</strong> screen, select <strong>Empty Activity</strong> and click <strong>Next</strong>. Activity</strong> and click <strong>Next</strong>. </li> </li> <div class="sidebox-wrapper"> <li>In the <strong>Customize the Activity</strong> screen, keep the default values <div class="sidebox"> <h3>Activities</h3> <p>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 <a href="{@docRoot}guide/components/activities.html"> Activities</a> for more information.</p> </div> </div> <li>Under <strong>Customize the Activity</strong>, accept the default values and click <strong>Finish</strong>. and click <strong>Finish</strong>. </ol> </ol> <p>Your Android project is now a basic "Hello World" app that contains some default files. Take a <p>After some processing, Android Studio opens and displays a "Hello World" app moment to review the most important of these:</p> with default files. You will add functionality to some of these files in the following lessons.</p> <p>Now take a moment to review the most important files. First, be sure that the <b>Project</b> window is open (select <b>View > Tool Windows > Project</b>) and the <b>Android</b> view is selected from the drop-down list at the top. You can then see the following files:</p> <dl> <dl> <dt><code>app/src/main/java/com.example.myfirstapp/MainActivity.java</code></dt> <dt><b>app > java > com.example.myfirstapp > MainActivity.java</b></dt> <dd>This file appears in Android Studio after the New Project wizard finishes. <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 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 and run the app, the {@link android.app.Activity} starts and loads the layout file that says "Hello World!"</dd> layout file that says "Hello World!"</dd> <dt><code>app/src/main/res/layout/activity_main.xml</code></dt> <dt><b>app > res > layout > activity_main.xml</b></dt> <dd>This XML file defines the layout of the activity. It contains a {@code TextView} <dd>This XML file defines the layout of the activity. It contains a {@code TextView} element with the text "Hello world!".</dd> element with the text "Hello world!".</dd> <dt><code>app/src/main/AndroidManifest.xml</code></dt> <dt><b>app > manifests > AndroidManifest.xml</b></dt> <dd>The <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">manifest file</a> describes <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 the fundamental characteristics of the app and defines each of its components. You'll revisit this file as you follow these lessons and add more components to your app.</dd> this file as you follow these lessons and add more components to your app.</dd> <dt><code>app/build.gradle</code></dt> <dt><b>Gradle Scripts > build.gradle</b></dt> <dd>Android Studio uses Gradle to compile and build your app. There is a <code>build.gradle</code> <dd>Android Studio uses Gradle to compile and build your app. There is a <code>build.gradle</code> file for each module of your project, as well as a <code>build.gradle</code> file for the entire file for each module of your project, as well as a <code>build.gradle</code> file for the entire project. Usually, you're only interested in the <code>build.gradle</code> file for the module, project. Usually, you're only interested in the <code>build.gradle</code> file for the module. in this case the <code>app</code> or application module. This is where your app's build dependencies in this case the <code>app</code> or application module. For more information about this file, are set, including the <code>defaultConfig</code> settings: see <a href="{@docRoot}studio/build/index.html">Building Your Project with Gradle</a>.</dd> <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. 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 in the New Project wizard.</li> <li><code>minSdkVersion</code> is the Minimum SDK version you specified during the New Project 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 thereby take advantage of new platform features. For more information, read <a href="{@docRoot}training/basics/supporting-devices/platforms.html">Supporting Different Platform Versions</a>.</li> </ul> <p>See <a href="{@docRoot}studio/build/index.html">Building Your Project with Gradle</a> for more information about Gradle.</p></dd> </dl> <p>Note also the <code>/res</code> subdirectories that contain the <a href="{@docRoot}guide/topics/resources/overview.html">resources</a> for your application:</p> <dl> <dt><code>drawable<em>-<density></em>/</code></dt> <dd>Directories for <a href="{@docRoot}guide/topics/resources/drawable-resource.html"> drawable resources</a>, other than launcher icons, designed 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_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> <dt><code>mipmap/</code></dt> <dd>Launcher icons reside in the {@code mipmap/} folder rather than the {@code drawable/} folders. This folder contains the {@code ic_launcher.png} image that appears when you run the default app.</dd> <dt><code>values/</code></dt> <dd>Directory for other XML files that contain a collection of resources, such as string and color definitions.</dd> </dl> </dl> <p> <p> Loading docs/html/training/basics/firstapp/running-app.jd +12 −10 Original line number Original line Diff line number Diff line Loading @@ -3,9 +3,7 @@ parent.title=Building Your First App parent.link=index.html parent.link=index.html trainingnavtop=true trainingnavtop=true page.tags=emulator page.tags=emulator helpoutsWidget=true @jd:body @jd:body Loading @@ -18,7 +16,7 @@ helpoutsWidget=true <ol> <ol> <li><a href="#RealDevice">Run on a Real Device</a></li> <li><a href="#RealDevice">Run on a Real Device</a></li> <li><a href="#Emulator">Run on the Emulator</a></li> <li><a href="#Emulator">Run on an Emulator</a></li> </ol> </ol> <h2>You should also read</h2> <h2>You should also read</h2> Loading @@ -34,8 +32,10 @@ helpoutsWidget=true <p>In the <a href="creating-project.html">previous lesson</a>, you created an <p>In the <a href="creating-project.html">previous lesson</a>, you created an Android project. The project contains a default app that displays Android project that displays "Hello World." You can now run the app on a real "Hello World". In this lesson, you will run the app on a device or emulator.</p> device or on an emulator. If you don't have a real device available, skip to <a href="#Emulator">Run on an Emulator</a>.</p> <h2 id="RealDevice">Run on a Real Device</h2> <h2 id="RealDevice">Run on a Real Device</h2> Loading Loading @@ -68,7 +68,7 @@ helpoutsWidget=true <p>Android Studio installs the app on your connected device and starts it.</p> <p>Android Studio installs the app on your connected device and starts it.</p> <h2 id="Emulator">Run on the Emulator</h2> <h2 id="Emulator">Run on an Emulator</h2> <p>Before you run your app on an emulator, you need to create an <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) <a href="{@docRoot}tools/devices/index.html">Android Virtual Device</a> (AVD) Loading @@ -82,12 +82,14 @@ helpoutsWidget=true <strong>Tools > Android > AVD Manager</strong>, or by clicking <strong>Tools > Android > AVD Manager</strong>, or by clicking the AVD Manager icon <img src="{@docRoot}images/tools/avd-manager-studio.png" 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> 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 <b>Your Virtual Devices</b> screen, click <strong>Create Virtual Device</strong>.</li> <li>In the Select Hardware page, select a phone device, such as Nexus 6, <li>In the <b>Select Hardware</b> screen, select a phone device, such as Nexus 6, then click <strong>Next</strong>. and then click <strong>Next</strong>. </li> </li> <li>In the Select Image page, choose the desired system image for the AVD and <li>In the <b>System Image</b> screen, choose the desired system image for the AVD and click <strong>Next</strong>. click <strong>Next</strong>. <p>If you don't have a particular system image installed, you can get it by clicking the <b>download</b> link.</p> </li> </li> <li>Verify the configuration settings (for your first AVD, leave all the <li>Verify the configuration settings (for your first AVD, leave all the settings as they are), and then click <strong>Finish</strong>. settings as they are), and then click <strong>Finish</strong>. Loading docs/html/training/basics/firstapp/starting-activity.jd +33 −11 Original line number Original line Diff line number Diff line Loading @@ -38,7 +38,7 @@ starts a new activity when the user clicks the Send button.</p> <h2 id="RespondToButton">Respond to the Send Button</h2> <h2 id="RespondToButton">Respond to the Send Button</h2> <ol> <ol> <li>In the file <code>res/layout/activity_main.xml</code>, add the <li>In the file <b>res > layout > activity_main.xml</b>, add the <a href="{@docRoot}reference/android/view/View.html#attr_android:onClick">{@code android:onClick}</a> <a href="{@docRoot}reference/android/view/View.html#attr_android:onClick">{@code android:onClick}</a> attribute to the {@link android.widget.Button <Button>} element as attribute to the {@link android.widget.Button <Button>} element as shown below: shown below: Loading @@ -52,7 +52,7 @@ starts a new activity when the user clicks the Send button.</p> method in your activity whenever a user clicks on the button.</p> method in your activity whenever a user clicks on the button.</p> </li> </li> <li>In the file <code>java/com.example.myfirstapp/MainActivity.java</code>, <li>In the file <b>java > com.example.myfirstapp > MainActivity.java</b>, add the <code>sendMessage()</code> method stub as shown below: add the <code>sendMessage()</code> method stub as shown below: <pre>public class MainActivity extends AppCompatActivity { <pre>public class MainActivity extends AppCompatActivity { Loading Loading @@ -85,7 +85,9 @@ starts a new activity when the user clicks the Send button.</p> <p>Next, you’ll fill in this method to read the contents of the text field and deliver that text to <p>Next, you’ll fill in this method to read the contents of the text field and deliver that text to another activity.</p> another activity.</p> <h2 id="BuildIntent">Build an Intent</h2> <h2 id="BuildIntent">Build an Intent</h2> <p>An {@link android.content.Intent} is an object that provides runtime binding <p>An {@link android.content.Intent} is an object that provides runtime binding between separate components (such as two activities). The between separate components (such as two activities). The {@link android.content.Intent} represents an app’s "intent to do something." {@link android.content.Intent} represents an app’s "intent to do something." Loading Loading @@ -113,13 +115,22 @@ another activity.</p> } } }</pre> }</pre> <p class="note"><strong>Note: </strong>Android Studio will display <p>Android Studio will display <b>Cannot <code>Cannot resolve symbol</code> errors because the code references classes resolve symbol</b> errors because this code references classes that are not like {@link android.content.Intent} and {@link android.widget.EditText} imported. You can solve some of these with Android Studio's "import class" that have not been imported. To import these classes, you can either 1) functionality by pressing Alt + Enter (or Option + Return on Mac). use Android Studio's "import class" functionality by pressing Alt + Enter Your imports should end up as the following:</p> (Option + Return on Mac) or 2) manually add import statements at the top of <pre> the file.</p> import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; </pre> <p>An error remains for <code>DisplayMessageActivity</code>, but that's okay; you'll fix that in the next section. <p>There’s a lot going on in <code>sendMessage()</code>, so let’s explain <p>There’s a lot going on in <code>sendMessage()</code>, so let’s explain what's going on.</p> what's going on.</p> Loading Loading @@ -150,6 +161,7 @@ another activity.</p> method starts an instance of the <code>DisplayMessageActivity</code> specified method starts an instance of the <code>DisplayMessageActivity</code> specified by the {@link android.content.Intent}. Now you need to create the class.</p> by the {@link android.content.Intent}. Now you need to create the class.</p> <h2 id="CreateActivity">Create the Second Activity</h2> <h2 id="CreateActivity">Create the Second Activity</h2> <ol> <ol> Loading @@ -169,7 +181,8 @@ another activity.</p> <li>Creates the corresponding layout file <code>activity_display_message.xml</code> <li>Creates the corresponding layout file <code>activity_display_message.xml</code> </li> </li> <li>Adds the required <li>Adds the required <a href="{@docRoot}guide/topics/manifest/activity-element.html"><activity></a> <a href="{@docRoot}guide/topics/manifest/activity-element.html" ><code><activity></code></a> element in <code>AndroidManifest.xml</code>. element in <code>AndroidManifest.xml</code>. </ul> </ul> Loading Loading @@ -199,7 +212,16 @@ protected void onCreate(Bundle savedInstanceState) { layout.addView(textView); layout.addView(textView); }</pre> }</pre> </li> </li> <li>Press Alt + Enter (option + return on Mac) to import missing classes.</li> <li>Press Alt + Enter (or Option + Return on Mac) to import missing classes. Your imports should end up as the following: <pre> import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.ViewGroup; import android.widget.TextView; </pre> </li> </ol> </ol> <p>There’s a lot going on here, so let’s explain:</p> <p>There’s a lot going on here, so let’s explain:</p> Loading Loading
docs/html/training/basics/firstapp/building-ui.jd +17 −17 Original line number Original line Diff line number Diff line Loading @@ -71,17 +71,17 @@ android.view.View} objects.</p> <h2 id="LinearLayout">Create a Linear Layout</h2> <h2 id="LinearLayout">Create a Linear Layout</h2> <ol> <ol> <li>From the <code>res/layout/</code> directory, open the <li>In Android Studio's <b>Project</b> window, open <b>app > res > <code>activity_main.xml</code> file. layout > activity_main.xml</b>. <p>This XML file defines the layout of your activity. It contains the <p>This XML file defines the layout of your activity. It contains the default "Hello World" text view.</p> default "Hello World" text view.</p> </li> </li> <li>When you open a layout file, you’re first shown the design editor in the <li>When you open a layout file, you’re first shown the design editor in the <a href="/studio/write/layout-editor.html">Layout Editor</a>. For this lesson, <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 you work directly with the XML, so click the <b>Text</b> tab at the bottom the text editor. of the window to switch to the text editor. </li> </li> <li>Replace the contents of the file with the following XML: <li>Delete everything and insert the following XML: <pre><?xml version="1.0" encoding="utf-8"?> <pre><?xml version="1.0" encoding="utf-8"?> <LinearLayout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android" Loading Loading @@ -138,6 +138,9 @@ href="{@docRoot}guide/topics/ui/declaring-layout.html">Layout</a> guide.</p> </LinearLayout> </LinearLayout> </pre> </pre> <p>Don't worry about the error that appears for <code>@string/edit_message</code>; you'll fix that soon.</p> <p>Here is a description of the attributes in the <p>Here is a description of the attributes in the {@link android.widget.EditText <EditText>} you added:</p> {@link android.widget.EditText <EditText>} you added:</p> Loading @@ -157,7 +160,7 @@ XML. It is followed by the resource type ({@code id} in this case), a slash, the <p>A resource object is a unique integer name that's associated with an app resource, <p>A resource object is a unique integer name that's associated with an app resource, such as a bitmap, layout file, or string.</p> such as a bitmap, layout file, or string.</p> <p>Every resource has a <p>Every resource has a corresponding resource object defined in your project's {@code gen/R.java} file. You can use the corresponding resource object defined in your project's {@code 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 object names in the {@code R} class to refer to your resources, such as when you need to specify a string value for the <a string value for the <a href="{@docRoot}reference/android/widget/TextView.html#attr_android:hint">{@code android:hint}</a> href="{@docRoot}reference/android/widget/TextView.html#attr_android:hint">{@code android:hint}</a> Loading @@ -174,7 +177,7 @@ href="{@docRoot}guide/topics/resources/providing-resources.html">Providing Resou <p>The plus sign (<code>+</code>) before the resource type is needed only when you're defining a <p>The plus sign (<code>+</code>) before the resource type is needed only when you're defining a resource ID for the first time. When you compile the app, 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 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 your project's {@code R.java} file that refers to the {@link android.widget.EditText} element. With the resource ID declared once this way, android.widget.EditText} element. With the resource ID declared once this way, other references to the ID do not 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 need the plus sign. Using the plus sign is necessary only when specifying a new resource ID and not Loading Loading @@ -211,10 +214,10 @@ the same name does not cause collisions.</p> <h2 id="Strings">Add String Resources</h2> <h2 id="Strings">Add String Resources</h2> <p>By default, your Android project includes a string resource file at <p>By default, your Android project includes a string resource file at <code>res/values/strings.xml</code>. Here, you'll add two new strings.</p> <b>res > values > strings.xml</b>. Here, you'll add two new strings.</p> <ol> <ol> <li>From the <code>res/values/</code> directory, open <code>strings.xml</code>.</li> <li>From the <b>Project</b> window, open <b>res > values > strings.xml</b>.</li> <li>Add two strings so that your file looks like this: <li>Add two strings so that your file looks like this: <pre><?xml version="1.0" encoding="utf-8"?> <pre><?xml version="1.0" encoding="utf-8"?> <resources> <resources> Loading Loading @@ -340,15 +343,12 @@ android.widget.LinearLayout}.</p> <h2>Run Your App</h2> <h2>Run Your App</h2> <p>This layout is applied by the default {@link android.app.Activity} class <p>To see how the app now looks on your device or emulator, that the SDK tools generated when you created the project.</p> click <strong>Run</strong> <p>To run the app and see the results, click <strong>Run 'app'</strong> <img src="{@docRoot}images/tools/as-run.png" <img src="{@docRoot}images/tools/as-run.png" style="vertical-align:baseline;margin:0; max-height:1em" /> in the style="vertical-align:baseline;margin:0; max-height:1em" /> in the toolbar.</p> toolbar.</p> <p>Continue to the <a href="starting-activity.html">next <p>To add app behaviors such as responding to a button and starting lesson</a> to learn how to respond to button presses, read content another activity, continue to the <a href="starting-activity.html">next from the text field, start another activity, and more.</p> lesson</a>.</p> No newline at end of file No newline at end of file
docs/html/training/basics/firstapp/creating-project.jd +34 −92 Original line number Original line Diff line number Diff line Loading @@ -31,129 +31,71 @@ next.link=running-app.html <ol> <ol> <li>In Android Studio, create a new project: <li>In Android Studio, create a new project: <ul> <ul> <li>If you don't have a project opened, in the <strong>Welcome</strong> screen, click <strong> <li>If you don't have a project opened, in the <strong>Welcome to Android Studio</strong> window, click <strong> New Project</strong>.</li> Start a new Android Studio project</strong>.</li> <li>If you have a project opened, from the <strong>File</strong> menu, select <strong>New <li>If you have a project opened, select <strong>File > New Project</strong>.</li> Project</strong>. The <em>Create New Project</em> screen appears.</li> </ul> </ul> </li> </li> <li>Fill out the fields on the screen. For <strong>Application Name</strong> <li>In the <b>New Project</b> screen, enter the following values:</p> 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> <ul> <li><strong>Application Name</strong> is the app name that appears to users.</li> <li><strong>Application Name</strong>: "My First App" </li> <li><strong>Company domain</strong> provides a qualifier that will be appended to the package <li><strong>Company Domain</strong>: "example.com"</li> 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 same rules as those for naming packages in the Java programming language). Your package name must be unique across all packages installed on the Android system. You can <strong> Edit</strong> this value independently from the application name or the company domain.</li> <li><strong>Project location</strong> is the directory on your system that holds the project files.</li> </ul> </ul> <p>Android Studio fills in the package name and project location for you, but you can edit these if you'd like. </li> </li> <li>Under <strong>Target Android Devices</strong>, accept the default values <li>Click <b>Next</b>.</li> and click <strong>Next</strong>. <li>In the <b>Target Android Devices</b> screen, keep the default values and <p>The Minimum Required SDK is the earliest version of Android that your app supports, click <b>Next</b>. indicated using the <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#ApiLevels"> <p>The <b>Minimum Required SDK</b> is the earliest version of Android that your app supports, which is indicated by 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 API level</a>. 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 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 app is possible only on newer versions of Android and it's not critical to the core feature set, you can enable the feature only when running on the versions that support it (as feature set, enable that feature only when running on the versions that support it (see discussed in <a href="{@docRoot}training/basics/supporting-devices/platforms.html"> <a href="{@docRoot}training/basics/supporting-devices/platforms.html"> Supporting Different Platform Versions</a>).</p> Supporting Different Platform Versions</a>).</p> </li> </li> <li>Under <strong>Add an Activity to Mobile</strong>, select <strong>Empty <li>In the <strong>Add an Activity to Mobile</strong> screen, select <strong>Empty Activity</strong> and click <strong>Next</strong>. Activity</strong> and click <strong>Next</strong>. </li> </li> <div class="sidebox-wrapper"> <li>In the <strong>Customize the Activity</strong> screen, keep the default values <div class="sidebox"> <h3>Activities</h3> <p>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 <a href="{@docRoot}guide/components/activities.html"> Activities</a> for more information.</p> </div> </div> <li>Under <strong>Customize the Activity</strong>, accept the default values and click <strong>Finish</strong>. and click <strong>Finish</strong>. </ol> </ol> <p>Your Android project is now a basic "Hello World" app that contains some default files. Take a <p>After some processing, Android Studio opens and displays a "Hello World" app moment to review the most important of these:</p> with default files. You will add functionality to some of these files in the following lessons.</p> <p>Now take a moment to review the most important files. First, be sure that the <b>Project</b> window is open (select <b>View > Tool Windows > Project</b>) and the <b>Android</b> view is selected from the drop-down list at the top. You can then see the following files:</p> <dl> <dl> <dt><code>app/src/main/java/com.example.myfirstapp/MainActivity.java</code></dt> <dt><b>app > java > com.example.myfirstapp > MainActivity.java</b></dt> <dd>This file appears in Android Studio after the New Project wizard finishes. <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 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 and run the app, the {@link android.app.Activity} starts and loads the layout file that says "Hello World!"</dd> layout file that says "Hello World!"</dd> <dt><code>app/src/main/res/layout/activity_main.xml</code></dt> <dt><b>app > res > layout > activity_main.xml</b></dt> <dd>This XML file defines the layout of the activity. It contains a {@code TextView} <dd>This XML file defines the layout of the activity. It contains a {@code TextView} element with the text "Hello world!".</dd> element with the text "Hello world!".</dd> <dt><code>app/src/main/AndroidManifest.xml</code></dt> <dt><b>app > manifests > AndroidManifest.xml</b></dt> <dd>The <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">manifest file</a> describes <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 the fundamental characteristics of the app and defines each of its components. You'll revisit this file as you follow these lessons and add more components to your app.</dd> this file as you follow these lessons and add more components to your app.</dd> <dt><code>app/build.gradle</code></dt> <dt><b>Gradle Scripts > build.gradle</b></dt> <dd>Android Studio uses Gradle to compile and build your app. There is a <code>build.gradle</code> <dd>Android Studio uses Gradle to compile and build your app. There is a <code>build.gradle</code> file for each module of your project, as well as a <code>build.gradle</code> file for the entire file for each module of your project, as well as a <code>build.gradle</code> file for the entire project. Usually, you're only interested in the <code>build.gradle</code> file for the module, project. Usually, you're only interested in the <code>build.gradle</code> file for the module. in this case the <code>app</code> or application module. This is where your app's build dependencies in this case the <code>app</code> or application module. For more information about this file, are set, including the <code>defaultConfig</code> settings: see <a href="{@docRoot}studio/build/index.html">Building Your Project with Gradle</a>.</dd> <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. 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 in the New Project wizard.</li> <li><code>minSdkVersion</code> is the Minimum SDK version you specified during the New Project 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 thereby take advantage of new platform features. For more information, read <a href="{@docRoot}training/basics/supporting-devices/platforms.html">Supporting Different Platform Versions</a>.</li> </ul> <p>See <a href="{@docRoot}studio/build/index.html">Building Your Project with Gradle</a> for more information about Gradle.</p></dd> </dl> <p>Note also the <code>/res</code> subdirectories that contain the <a href="{@docRoot}guide/topics/resources/overview.html">resources</a> for your application:</p> <dl> <dt><code>drawable<em>-<density></em>/</code></dt> <dd>Directories for <a href="{@docRoot}guide/topics/resources/drawable-resource.html"> drawable resources</a>, other than launcher icons, designed 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_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> <dt><code>mipmap/</code></dt> <dd>Launcher icons reside in the {@code mipmap/} folder rather than the {@code drawable/} folders. This folder contains the {@code ic_launcher.png} image that appears when you run the default app.</dd> <dt><code>values/</code></dt> <dd>Directory for other XML files that contain a collection of resources, such as string and color definitions.</dd> </dl> </dl> <p> <p> Loading
docs/html/training/basics/firstapp/running-app.jd +12 −10 Original line number Original line Diff line number Diff line Loading @@ -3,9 +3,7 @@ parent.title=Building Your First App parent.link=index.html parent.link=index.html trainingnavtop=true trainingnavtop=true page.tags=emulator page.tags=emulator helpoutsWidget=true @jd:body @jd:body Loading @@ -18,7 +16,7 @@ helpoutsWidget=true <ol> <ol> <li><a href="#RealDevice">Run on a Real Device</a></li> <li><a href="#RealDevice">Run on a Real Device</a></li> <li><a href="#Emulator">Run on the Emulator</a></li> <li><a href="#Emulator">Run on an Emulator</a></li> </ol> </ol> <h2>You should also read</h2> <h2>You should also read</h2> Loading @@ -34,8 +32,10 @@ helpoutsWidget=true <p>In the <a href="creating-project.html">previous lesson</a>, you created an <p>In the <a href="creating-project.html">previous lesson</a>, you created an Android project. The project contains a default app that displays Android project that displays "Hello World." You can now run the app on a real "Hello World". In this lesson, you will run the app on a device or emulator.</p> device or on an emulator. If you don't have a real device available, skip to <a href="#Emulator">Run on an Emulator</a>.</p> <h2 id="RealDevice">Run on a Real Device</h2> <h2 id="RealDevice">Run on a Real Device</h2> Loading Loading @@ -68,7 +68,7 @@ helpoutsWidget=true <p>Android Studio installs the app on your connected device and starts it.</p> <p>Android Studio installs the app on your connected device and starts it.</p> <h2 id="Emulator">Run on the Emulator</h2> <h2 id="Emulator">Run on an Emulator</h2> <p>Before you run your app on an emulator, you need to create an <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) <a href="{@docRoot}tools/devices/index.html">Android Virtual Device</a> (AVD) Loading @@ -82,12 +82,14 @@ helpoutsWidget=true <strong>Tools > Android > AVD Manager</strong>, or by clicking <strong>Tools > Android > AVD Manager</strong>, or by clicking the AVD Manager icon <img src="{@docRoot}images/tools/avd-manager-studio.png" 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> 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 <b>Your Virtual Devices</b> screen, click <strong>Create Virtual Device</strong>.</li> <li>In the Select Hardware page, select a phone device, such as Nexus 6, <li>In the <b>Select Hardware</b> screen, select a phone device, such as Nexus 6, then click <strong>Next</strong>. and then click <strong>Next</strong>. </li> </li> <li>In the Select Image page, choose the desired system image for the AVD and <li>In the <b>System Image</b> screen, choose the desired system image for the AVD and click <strong>Next</strong>. click <strong>Next</strong>. <p>If you don't have a particular system image installed, you can get it by clicking the <b>download</b> link.</p> </li> </li> <li>Verify the configuration settings (for your first AVD, leave all the <li>Verify the configuration settings (for your first AVD, leave all the settings as they are), and then click <strong>Finish</strong>. settings as they are), and then click <strong>Finish</strong>. Loading
docs/html/training/basics/firstapp/starting-activity.jd +33 −11 Original line number Original line Diff line number Diff line Loading @@ -38,7 +38,7 @@ starts a new activity when the user clicks the Send button.</p> <h2 id="RespondToButton">Respond to the Send Button</h2> <h2 id="RespondToButton">Respond to the Send Button</h2> <ol> <ol> <li>In the file <code>res/layout/activity_main.xml</code>, add the <li>In the file <b>res > layout > activity_main.xml</b>, add the <a href="{@docRoot}reference/android/view/View.html#attr_android:onClick">{@code android:onClick}</a> <a href="{@docRoot}reference/android/view/View.html#attr_android:onClick">{@code android:onClick}</a> attribute to the {@link android.widget.Button <Button>} element as attribute to the {@link android.widget.Button <Button>} element as shown below: shown below: Loading @@ -52,7 +52,7 @@ starts a new activity when the user clicks the Send button.</p> method in your activity whenever a user clicks on the button.</p> method in your activity whenever a user clicks on the button.</p> </li> </li> <li>In the file <code>java/com.example.myfirstapp/MainActivity.java</code>, <li>In the file <b>java > com.example.myfirstapp > MainActivity.java</b>, add the <code>sendMessage()</code> method stub as shown below: add the <code>sendMessage()</code> method stub as shown below: <pre>public class MainActivity extends AppCompatActivity { <pre>public class MainActivity extends AppCompatActivity { Loading Loading @@ -85,7 +85,9 @@ starts a new activity when the user clicks the Send button.</p> <p>Next, you’ll fill in this method to read the contents of the text field and deliver that text to <p>Next, you’ll fill in this method to read the contents of the text field and deliver that text to another activity.</p> another activity.</p> <h2 id="BuildIntent">Build an Intent</h2> <h2 id="BuildIntent">Build an Intent</h2> <p>An {@link android.content.Intent} is an object that provides runtime binding <p>An {@link android.content.Intent} is an object that provides runtime binding between separate components (such as two activities). The between separate components (such as two activities). The {@link android.content.Intent} represents an app’s "intent to do something." {@link android.content.Intent} represents an app’s "intent to do something." Loading Loading @@ -113,13 +115,22 @@ another activity.</p> } } }</pre> }</pre> <p class="note"><strong>Note: </strong>Android Studio will display <p>Android Studio will display <b>Cannot <code>Cannot resolve symbol</code> errors because the code references classes resolve symbol</b> errors because this code references classes that are not like {@link android.content.Intent} and {@link android.widget.EditText} imported. You can solve some of these with Android Studio's "import class" that have not been imported. To import these classes, you can either 1) functionality by pressing Alt + Enter (or Option + Return on Mac). use Android Studio's "import class" functionality by pressing Alt + Enter Your imports should end up as the following:</p> (Option + Return on Mac) or 2) manually add import statements at the top of <pre> the file.</p> import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; </pre> <p>An error remains for <code>DisplayMessageActivity</code>, but that's okay; you'll fix that in the next section. <p>There’s a lot going on in <code>sendMessage()</code>, so let’s explain <p>There’s a lot going on in <code>sendMessage()</code>, so let’s explain what's going on.</p> what's going on.</p> Loading Loading @@ -150,6 +161,7 @@ another activity.</p> method starts an instance of the <code>DisplayMessageActivity</code> specified method starts an instance of the <code>DisplayMessageActivity</code> specified by the {@link android.content.Intent}. Now you need to create the class.</p> by the {@link android.content.Intent}. Now you need to create the class.</p> <h2 id="CreateActivity">Create the Second Activity</h2> <h2 id="CreateActivity">Create the Second Activity</h2> <ol> <ol> Loading @@ -169,7 +181,8 @@ another activity.</p> <li>Creates the corresponding layout file <code>activity_display_message.xml</code> <li>Creates the corresponding layout file <code>activity_display_message.xml</code> </li> </li> <li>Adds the required <li>Adds the required <a href="{@docRoot}guide/topics/manifest/activity-element.html"><activity></a> <a href="{@docRoot}guide/topics/manifest/activity-element.html" ><code><activity></code></a> element in <code>AndroidManifest.xml</code>. element in <code>AndroidManifest.xml</code>. </ul> </ul> Loading Loading @@ -199,7 +212,16 @@ protected void onCreate(Bundle savedInstanceState) { layout.addView(textView); layout.addView(textView); }</pre> }</pre> </li> </li> <li>Press Alt + Enter (option + return on Mac) to import missing classes.</li> <li>Press Alt + Enter (or Option + Return on Mac) to import missing classes. Your imports should end up as the following: <pre> import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.ViewGroup; import android.widget.TextView; </pre> </li> </ol> </ol> <p>There’s a lot going on here, so let’s explain:</p> <p>There’s a lot going on here, so let’s explain:</p> Loading