From ae47d9d4fe28b9c67e9669ecd2f534e27a921d30 Mon Sep 17 00:00:00 2001
From: Mark Lu
res/layout
directory, open the {@code content_my.xml}
-file.
-The BlankActivity template you chose when you created this project includes the
-content_my.xml
file with a {@link android.widget.RelativeLayout} root view and a
-{@link android.widget.TextView} child view.
In Android Studio, when you open a layout file, youâre first shown - the Preview pane. Clicking elements in this pane opens the WYSIWYG tools in the Design pane. For - this lesson, youâre going to work directly with the XML.
"horizontal"
.The result looks like this:
- --<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" - xmlns:app="http://schemas.android.com/apk/res-auto" +
res/layout/
directory, open the
+ activity_main.xml
file.
+ This XML file defines the layout of your activity. It contains the + default "Hello World" text view.
+<?xml version="1.0" encoding="utf-8"?> +<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"> + android:orientation="horizontal"> +</LinearLayout>+
{@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 Layout guide.
As with every {@link android.view.View} object, you must define certain XML attributes to specify -the {@link android.widget.EditText} object's properties.
- -content_my.xml
file, within the
-{@link android.widget.LinearLayout <LinearLayout>} element, define an
-{@link android.widget.EditText <EditText>} element with the id
attribute
-set to @+id/edit_message
.layout_width
and layout_height
attributes as
-wrap_content
.hint
attribute as a string object named edit_message
.The {@link android.widget.EditText <EditText>} element should read as follows:
+In the activity_main.xml
file, within the
+{@link android.widget.LinearLayout <LinearLayout>} element, add the following
+{@link android.widget.EditText <EditText>} element:
-<EditText android:id="@+id/edit_message" - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:hint="@string/edit_message" /> +<LinearLayout + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="horizontal"> + <EditText android:id="@+id/edit_message" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:hint="@string/edit_message" /> +</LinearLayout>-Here are the {@link android.widget.EditText <EditText>} attributes you added:
+Here is a description of the attributes in the + {@link android.widget.EditText <EditText>} you added:
By default, your Android project includes a string resource file at
-res/values/strings.xml
. Here, you'll add a new string named
-"edit_message"
and set the value to "Enter a message."
res/values/strings.xml
. Here, you'll add two new strings.
res/values
directory, open strings.xml
."edit_message"
with the value, "Enter a message".
-"button_send"
with the value, "Send".
-You'll create the button that uses this string in the next section.
-The result for strings.xml
looks like this:
-<?xml version="1.0" encoding="utf-8"?> +
res/values/
directory, open strings.xml
.<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">My First App</string> - <string name="edit_message">Enter a message</string> - <string name="button_send">Send</string> - <string name="action_settings">Settings</string> + <string name="edit_message">Enter a message</string> + <string name="button_send">Send</string> </resources>+
For text in the user interface, always specify each string as a resource. String resources allow you to manage all UI text in a single location, @@ -260,40 +240,22 @@ class.
res/layout
directory, edit the content_my.xml
-file."wrap_content"
so
-the button is only as big as necessary to fit the button's text label.button_send
string
-resource you defined in the previous section.Your {@link android.widget.LinearLayout <LinearLayout>} should look like this:
- --<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" - xmlns:app="http://schemas.android.com/apk/res-auto" +Go back to the
+activity_main.xml
file and add a button after the + {@link android.widget.EditText <EditText>}. Your file should look like this:<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"> + android:layout_height="match_parent"> <EditText android:id="@+id/edit_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/edit_message" /> - <Button + <Button android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="@string/button_send" /> + android:text="@string/button_send" /> </LinearLayout>@@ -302,7 +264,7 @@ resource you defined in the previous section. attribute, because it won't be referenced from the activity code.The layout is currently designed so that both the {@link android.widget.EditText} and {@link -android.widget.Button} widgets are only as big as necessary to fit their content, as Figure 2 shows. +android.widget.Button} widgets are only as big as necessary to fit their content, as figure 2 shows.
@@ -334,53 +296,36 @@ given the space they require.Make the Input Box Fill in the Screen Width
-To fill the remaining space in your layout with the {@link android.widget.EditText} element, do -the following:
- --
- -- In the
-content_my.xml
file, assign the -{@link android.widget.EditText <EditText>} element'slayout_weight
attribute a value -of1
.- Also, assign {@link android.widget.EditText <EditText>} element's
-layout_width
-attribute a value of0dp
. +In
activity_main.xml
, modify the + {@link android.widget.EditText <EditText>} so that the attributes look like + this:-<EditText - android:layout_weight="1" - android:layout_width="0dp" - ... /> +<EditText android:id="@+id/edit_message" + android:layout_weight="1" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:hint="@string/edit_message" />-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 +
Setting the width to zero (0dp) improves layout performance because using
-"wrap_content"
as the width requires the system to calculate a width that is ultimately irrelevant because the weight value requires another width calculation to fill the remaining space.Figure 3 -shows the result when you assign all weight to the {@link android.widget.EditText} element.
- -Hereâs how your complete
+content_my.xml
layout file should now look:Hereâs how your complete
-activity_main.xml
layout file should now look:-<?xml version="1.0" encoding="utf-8"?> +<?xml version="1.0" encoding="utf-8"?> <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"> + android:layout_height="match_parent"> <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.Continue to the next lesson to learn how to respond to button presses, read content -from the text field, start another activity, and more.
- - - +from the text field, start another activity, and more. \ No newline at end of file diff --git a/docs/html/training/basics/firstapp/creating-project.jd b/docs/html/training/basics/firstapp/creating-project.jd index 4c2155b36314..cad32bf077d8 100644 --- a/docs/html/training/basics/firstapp/creating-project.jd +++ b/docs/html/training/basics/firstapp/creating-project.jd @@ -14,36 +14,19 @@ next.link=running-app.html--This lesson teaches you to
- - -You should also read
An Android project contains all the files that comprise the source code for your Android -app.
- -This lesson -shows how to create a new project either using Android Studio or using the -SDK tools from a command line.
- -Note: You should already have Android Studio or the Android SDK -command-line tools installed. If not, download them before you start this -lesson.
- - -Create a Project with Android Studio
+This lesson shows you how to create a new Android project with + Android Studio and describes some + of the files in the project.
- In Android Studio, create a new project: @@ -54,11 +37,12 @@ lesson. Project. The Create New Project screen appears.
-- Fill out the fields on the screen, and click Next. -
It is easier to follow these lessons if you use the same values as shown.
+- Fill out the fields on the screen. For Application Name + use "My First App". For Company Domain, use "example.com". + For the other fields, use the default values and click Next +
-Here's a brief explanation of each field:
-
- Application Name is the app name that appears to users. - For this project, use "My First App."
+- Application Name is the app name that appears to users.
- Company domain provides a qualifier that will be appended to the package name; Android Studio will remember this qualifier for each new project you create.
- Package name is the fully qualified name for the project (following the @@ -70,9 +54,8 @@ lesson. files.
- Under Select the form factors your app will run on, check the box for - Phone and Tablet.
-- For Minimum SDK, select API 8: Android 2.2 (Froyo). +
- Under Target Android Devices, accept the default values + and click Next.
-The Minimum Required SDK is the earliest version of Android that your app supports, indicated using the API level. To support as many devices as possible, you should set this to the lowest @@ -80,8 +63,13 @@ lesson.
app is possible only on newer versions of Android and it's not critical to the app's core feature set, you can enable the feature only when running on the versions that support it (as discussed in - Supporting Different Platform Versions).- Leave all of the other options (TV, Wear, and Glass) unchecked and click Next.
+ Supporting Different Platform Versions). + + +- Under Add an Activity to Mobile, select Empty + Activity and click Next. +
+-Activities
@@ -93,43 +81,25 @@ lesson. Activities for more information.- Under Add an activity to <template>, - select Basic Activity and click Next. -
- -- Under Customize the Activity, change the - Activity Name to MyActivity. The Layout - Name changes to activity_my, and the Title - to MyActivity. The Menu Resource Name is - menu_my. -
-- Click the Finish button to create the project. -
+- Under Customize the Activity, accept the default values + and click Finish.
Your Android project is now a basic "Hello World" app that contains some default files. Take a moment to review the most important of these:
app/src/main/res/layout/activity_my.xml
app/src/main/res/layout/content_my.xml
app/src/main/java/com.mycompany.myfirstapp/MyActivity.java
app/src/main/java/com.example.myfirstapp/MainActivity.java
app/src/main/res/layout/activity_main.xml
app/src/main/AndroidManifest.xml
compiledSdkVersion
is the platform version against which you will compile
your app. By default, this is set to the latest version of Android available in your SDK.
- (It should be Android 4.1 or greater; if you don't have such a version available, you must
- install one using the SDK Manager.)
+ 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.applicationId
is the fully qualified package name for your application that
- you specified during the New Project workflow.minSdkVersion
is the Minimum SDK version you specified during the New Project
- workflow. This is the earliest version of the Android SDK that your app supports.targetSdkVersion
indicates the highest version of Android with which you have
tested your application. As new versions of Android become available, you should
test your app on the new version and update this value to match the latest API level and
@@ -172,8 +142,8 @@ moment to review the most important of these:
for various densities.
layout/
menu/
Welcome to Android application development!
-This class teaches you how to build your first Android app. Youâll 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.
- -Before you start this class, be sure you have your development environment set up. You need -to:
-Note: 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.
- -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.
+This class teaches you how to build your first Android app. Youâll 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.
+ +Before you start this class, download and install + Android Studio.
\ No newline at end of file diff --git a/docs/html/training/basics/firstapp/running-app.jd b/docs/html/training/basics/firstapp/running-app.jd index 21fb64d83798..e8098711b9a9 100755 --- a/docs/html/training/basics/firstapp/running-app.jd +++ b/docs/html/training/basics/firstapp/running-app.jd @@ -26,7 +26,6 @@ helpoutsWidget=true @@ -34,27 +33,20 @@ helpoutsWidget=true -If you followed the previous lesson to create an -Android project, it includes a default set of "Hello World" source files that allow you to -immediately run the app.
- -How you run your app depends on two things: whether you have a real 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.
+In the previous lesson, 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.
If you have a device running Android, here's how to install and run your app.
- -Set up your device as follows:
Note: On Android 4.2 and newer, Developer options is hidden by default. To make it available, go @@ -63,73 +55,61 @@ line tools.
Run the app from Android Studio as follows:
+Android Studio installs the app on your connected device and starts it.
Whether you're using Android Studio or the command line, to run your app on the emulator you need -to first create an Android Virtual Device (AVD). An -AVD is a device configuration for the Android emulator that allows you to model a specific -device.
- +Before you run your app on an emulator, you need to create an + Android Virtual Device (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.
-Create an AVD Definition as follows:
sdk/
and execute:
- tools/android avd-
Note: 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.
-For more information about using AVDs, see -Managing AVDs with AVD Manager.
+Create and Manage Virtual Devices. -Run the app from Android Studio as follows:
It can take a few minutes for the emulator to load itself. You may have to unlock the screen. +
It can take a few minutes for the emulator to start. You may have to unlock the screen. When you do, My First App appears on the emulator screen.
That's how you build and run your Android app on the emulator! To start developing, continue to the next -lesson.
- - +lesson. \ No newline at end of file diff --git a/docs/html/training/basics/firstapp/starting-activity.jd b/docs/html/training/basics/firstapp/starting-activity.jd index e84e17e5d867..ebf42cbe0438 100644 --- a/docs/html/training/basics/firstapp/starting-activity.jd +++ b/docs/html/training/basics/firstapp/starting-activity.jd @@ -19,9 +19,7 @@ helpoutsWidget=trueAfter completing the previous lesson, you have an app that
shows an activity (a single screen) with a text field and a button. In this lesson, youâll add some
-code to MyActivity
that
+code to MainActivity
that
starts a new activity when the user clicks the Send button.
res/layout
directory, edit the content_my.xml
-file.-<Button - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:text="@string/button_send" - android:onClick="sendMessage" /> -- -
The {@code
-android:onClick} attributeâs value, "sendMessage"
, is the name of a method in your
-activity that the system calls when the user clicks the button.
java/com.mycompany.myfirstapp
directory, open the MyActivity.java
file.MyActivity
class, add the {@code sendMessage()} method stub shown
-below.
-
-
--/** Called when the user clicks the Send button */ -public void sendMessage(View view) { - // Do something in response to button -} -- -
In order for the system to match this method to the method name given to {@code android:onClick}, -the signature must be exactly as shown. Specifically, the method must:
- -Next, youâll fill in this method to read the contents of the text field and deliver that text to -another activity.
- -MyActivity.java
, inside the {@code sendMessage()} method, create an
-{@link android.content.Intent} to start an activity called {@code DisplayMessageActivity} with the
-following code:
-
-
--public void sendMessage(View view) { - Intent intent = new Intent(this, DisplayMessageActivity.class); -} -- -
An {@link android.content.Intent} is an object that provides runtime binding between separate -components (such as two activities). The {@link android.content.Intent} represents an -appâs "intent to do something." You can use intents for a wide -variety of tasks, but most often theyâre used to start another activity. For more information, see -Intents and Intent Filters.
-Note: The reference to {@code DisplayMessageActivity} -will raise an error if youâre using an IDE such as Android Studio because the class doesnât exist yet. -Ignore the error for now; youâll create the class soon.
- -The constructor used here takes two parameters:
-Android Studio indicates that you must import the {@link android.content.Intent} class.
- --import android.content.Intent; --
Tip: In Android Studio, press Alt + Enter (option + return on Mac) - to import missing classes.
--public void sendMessage(View view) { - Intent intent = new Intent(this, DisplayMessageActivity.class); - EditText editText = (EditText) findViewById(R.id.edit_message); -} --
In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes.
-message
variable, and use the
-{@link android.content.Intent#putExtra putExtra()} method to add its text value to the intent.
-
--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); -} -- -
An {@link android.content.Intent} can carry data types as key-value -pairs called extras. The {@link android.content.Intent#putExtra putExtra()} method takes the -key name in the first parameter and the value in the second parameter.
- --public class MyActivity extends AppCompatActivity { - public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE"; - ... -} -- -
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.
- -With this new code, the complete {@code sendMessage()} method that's invoked by the Send -button now looks like this:
- --/** 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); -} -- -
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.
- - - - +res/layout/activity_main.xml
, add the
+ {@code android:onClick}
+ attribute to the {@link android.widget.Button <Button>} element as
+ shown below:
+ <Button + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="@string/button_send" + android:onClick="sendMessage" /> ++
This attribute tells the system to call the sendMessage()
+ method in your activity whenever a user clicks on the button.
java/com.example.myfirstapp/MainActivity.java
,
+ add the sendMessage()
method stub as shown below:
-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.
+public class MainActivity extends AppCompatActivity { + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + } --Create a new activity using Android Studio
+ /** Called when the user clicks the Send button */ + public void sendMessage(View view) { + // Do something in response to button + } +}
Android Studio includes a stub for the -{@link android.app.Activity#onCreate onCreate()} method when you create a new activity. The -New Android Activity window appears.
+In order for the system to match this method to the method name given to {@code android:onClick}, + the signature must be exactly as shown. Specifically, the method must:
-java
directory, select the package,
- com.mycompany.myfirstapp, right-click, and select
- New > Activity > Blank Activity.Click Finish.
-The class already includes an implementation of the required -{@link android.app.Activity#onCreate onCreate()} method. You update the implementation of this -method later.
- -If you're developing with Android Studio, you can run the app now, but not much happens. -Clicking the Send button starts the second activity, but it uses -a default "Hello world" layout provided by the template. You'll soon update the -activity to instead display a custom text view.
- - -If you're using a different IDE or the command line tools, do the following:
+Next, youâll fill in this method to read the contents of the text field and deliver that text to +another activity.
-src/
-directory, next to the original {@code MyActivity.java} file.An {@link android.content.Intent} is an object that provides runtime binding + between separate components (such as two activities). The + {@link android.content.Intent} represents an appâs "intent to do something." + You can use intents for a wide variety of tasks, but in this lesson, your intent + starts another activity.
--public class DisplayMessageActivity extends AppCompatActivity { ++In
+MainActivity.java
, add the code shown below to +sendMessage()
:public class MainActivity extends AppCompatActivity { + public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; @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); } - @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) { + 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); } +}- /** - * A placeholder fragment containing a simple view. - */ - public static class PlaceholderFragment extends Fragment { - - public PlaceholderFragment() { } +Note: Android Studio will display +
- @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View rootView = inflater.inflate(R.layout.fragment_display_message, - container, false); - return rootView; - } - } -} -Cannot resolve symbol
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.
Thereâs a lot going on in sendMessage()
, so letâs explain
+ what's going on.
Note: If you are using an IDE other than Android Studio, your project -does not contain the {@code activity_display_message} layout that's requested by -{@link android.app.Activity#setContentView setContentView()}. That's OK because -you will update this method later and won't be using that layout.
+The {@link android.content.Intent} constructor takes two parameters:
+Note: The reference to
+ DisplayMessageActivity
will raise an error in Android Studio
+ because the class doesnât exist yet. Ignore the error for now; youâll
+ create the class soon.
The {@link android.content.Intent#putExtra(String, String) putExtra()}
+ method adds the EditText
's value to the intent. An Intent
+ can carry data types as key-value pairs called extras. Your key is a
+ public constant EXTRA_MESSAGE
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.
-<resources> - ... - <string name="title_activity_display_message">My Message</string> -</resources> --
The {@link android.app.Activity#startActivity(Intent) startActivity()}
+ method starts an instance of the DisplayMessageActivity
specified
+ by the {@link android.content.Intent}. Now you need to create the class.
AndroidManifest.xml
, within the Application
-element, add the
-{@code -<application ... > - ... - <activity - android:name="com.mycompany.myfirstapp.DisplayMessageActivity" - android:label="@string/title_activity_display_message" - android:parentActivityName="com.mycompany.myfirstapp.MyActivity" > - <meta-data - android:name="android.support.PARENT_ACTIVITY" - android:value="com.mycompany.myfirstapp.MyActivity" /> - </activity> -</application> -+
The {@code
-android:parentActivityName} attribute declares the name of this activity's parent activity
-within the app's logical hierarchy. The system uses this value
-to implement default navigation behaviors, such as Up navigation on
-Android 4.1 (API level 16) and higher. You can provide the same navigation behaviors for
-older versions of Android by using the
-Support Library and adding
-the {@code
-
Note: Your Android SDK should already include -the latest Android Support Library, which you installed during the -Adding SDK Packages step. -When using the templates in Android Studio, the Support Library is automatically added to your app project -(you can see the library's JAR file listed under Android Dependencies). If you're not using -Android Studio, you need to manually add the library to your project—follow the guide for setting up the Support Library -then return here.
- -If you're using a different IDE than Android Studio, don't worry that the app won't yet compile. -You'll soon update the activity to display a custom text view.
- - -Every {@link android.app.Activity} is invoked by an {@link android.content.Intent}, regardless of -how the user navigated there. You can get the {@link android.content.Intent} that started your -activity by calling {@link android.app.Activity#getIntent()} and retrieve the data contained -within the intent.
+Android Studio automatically does three things:
+DisplayMessageActivity.java
with an
+ implementation of the required {@link android.app.Activity#onCreate(Bundle) onCreate()}
+ method.activity_display_message.xml
+ AndroidManifest.xml
.
+java/com.mycompany.myfirstapp
directory, edit the
- {@code DisplayMessageActivity.java} file.-Intent intent = getIntent(); --
In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes.
--String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE); --
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.
Now you will modify the second activity to display the message that was passed +by the first activity.
-< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" -... -android:id="@+id/content"> -</RelativeLayout> --
-TextView textView = new TextView(this); --
-textView.setTextSize(40); -textView.setText(message); --
-RelativeLayout layout = (RelativeLayout) findViewById(R.id.content); -layout.addView(textView); --
In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes.
-The complete {@link android.app.Activity#onCreate onCreate()} method for {@code -DisplayMessageActivity} now looks like this:
- --@Override +
onCreate()
method:
+ @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() { - @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); -+} +
Thereâs a lot going on here, so letâs explain:
+ +TextView
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.Note: The XML layout generated by previous
+ versions of Android Studio might not include the android:id
+ attribute. The call findViewById()
will fail if the layout
+ does not have the android:id
attribute. If this is the case,
+ open activity_display_message.xml
and add the attribute
+ android:id="@+id/activity_display_message"
to the layout element.
+
You can now run the app. When it opens, type a message in the text field, and click Send. The second activity replaces the first one on the screen, showing @@ -513,8 +234,4 @@ the message you entered in the first activity.
That's it, you've built your first Android app!
-To learn more, follow the link below to the next class.
- - - - +To learn more, follow the link below to the next class.
\ No newline at end of file -- 2.11.0