- •Getting Started
- •Building Your First App
- •Dependencies and prerequisites
- •Creating an Android Project
- •This lesson teaches you to
- •You should also read
- •Create a Project with Eclipse
- •Create a Project with Command Line Tools
- •Running Your App
- •This lesson teaches you to
- •You should also read
- •Run on a Real Device
- •Run on the Emulator
- •Building a Simple User Interface
- •This lesson teaches you to
- •You should also read
- •Alternative Layouts
- •Create a Linear Layout
- •Add a Text Field
- •About resource objects
- •Add String Resources
- •Add a Button
- •Make the Input Box Fill in the Screen Width
- •Installing the sdk
- •Build an Intent
- •Sending an intent to other apps
- •Start the Second Activity
- •Create the Second Activity
- •Add the title string
- •Add it to the manifest
- •Receive the Intent
- •Display the Message
Add the title string
If you used Eclipse, you can skip to the next section, because the template provides the title string for the new activity.
If you're using an IDE other than Eclipse, add the new activity's title to the strings.xml file:
<resources> ... <string name="title_activity_display_message">My Message</string> </resources>
Add it to the manifest
All activities must be declared in your manifest file, AndroidManifest.xml, using an <activity> element.
When you use the Eclipse tools to create the activity, it creates a default entry. If you're using a different IDE, you need to add the manifest entry yourself. It should look like this:
<application ... > ... <activity android:name="com.example.myfirstapp.DisplayMessageActivity" android:label="@string/title_activity_display_message" android:parentActivityName="com.example.myfirstapp.MainActivity" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.myfirstapp.MainActivity" /> </activity> </application>
The 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 <meta-data> element as shown here.
Note: Your Android SDK should already include the latest Android Support Library. It's included with the ADT Bundle but if you're using a different IDE, you should have installed it during the Adding Platforms and Packages step. When using the templates in Eclipse, the Support Library is automatically added to your app project (you can see the library's JAR file listed under Android Dependencies). If you're not using Eclipse, you need to manually add the library to your project—follow the guide for setting up the Support Library then return here.
If you're developing with Eclipse, you can run the app now, but not much happens. Clicking the Send button starts the second activity but it uses a default "Hello world" layout provided by the template. You'll soon update the activity to instead display a custom text view, so if you're using a different IDE, don't worry that the app won't yet compile.
Receive the Intent
Every Activity is invoked by an Intent, regardless of how the user navigated there. You can get the Intent that started your activity by calling getIntent() and retrieve the data contained within it.
In the DisplayMessageActivity class’s onCreate() method, get the intent and extract the message delivered by MainActivity:
Intent intent = getIntent(); String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
Display the Message
To show the message on the screen, create a TextView widget and set the text using setText(). Then add the TextView as the root view of the activity’s layout by passing it to setContentView().
The complete onCreate() method for DisplayMessageActivity now looks like this:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the message from the intent Intent intent = getIntent(); String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); // Create the text view TextView textView = new TextView(this); textView.setTextSize(40); textView.setText(message); // Set the text view as the activity layout setContentView(textView); }
You can now run the app. When it opens, type a message in the text field, click Send, and the message appears on the second activity.
Figure 2. Both activities in the final app, running on Android 4.0.
That's it, you've built your first Android app!
To learn more about building Android apps, continue to follow the basic training classes. The next class is Managing the Activity Lifecycle.
