
- •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
Installing the sdk
After completing the previous lesson, you have an app that shows an activity (a single screen) with a text field and a button. In this lesson, you’ll add some code to MainActivity that starts a new activity when the user clicks the Send button.
Respond to the Send Button
To respond to the button's on-click event, open the activity_main.xml layout file and add the android:onClick attribute to the <Button> element:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_send" android:onClick="sendMessage" />
The 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.
Open the MainActivity class (located in the project's src/ directory) and add the corresponding method:
/** Called when the user clicks the Send button */ public void sendMessage(View view) { // Do something in response to button }
This requires that you import the View class:
import android.view.View;
Tip: In Eclipse, press Ctrl + Shift + O to import missing classes (Cmd + Shift + O on Mac).
In order for the system to match this method to the method name given to android:onClick, the signature must be exactly as shown. Specifically, the method must:
Be public
Have a void return value
Have a View as the only parameter (this will be the View that was clicked)
Next, you’ll fill in this method to read the contents of the text field and deliver that text to another activity.
Build an Intent
An Intent is an object that provides runtime binding between separate components (such as two activities). The 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.
Inside the sendMessage() method, create an Intent to start an activity called DisplayMessageActivity:
Intent intent = new Intent(this, DisplayMessageActivity.class);
The constructor used here takes two parameters:
A Context as its first parameter (this is used because the Activity class is a subclass of Context)
The Class of the app component to which the system should deliver the Intent (in this case, the activity that should be started)
Sending an intent to other apps
The intent created in this lesson is what's considered an explicit intent, because the Intent specifies the exact app component to which the intent should be given. However, intents can also be implicit, in which case the 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 Intent parameters. For more information, see the class about Interacting with Other Apps.
Note: The reference to DisplayMessageActivity will raise an error if you’re using an IDE such as Eclipse because the class doesn’t exist yet. Ignore the error for now; you’ll create the class soon.
An intent not only allows you to start another activity, but it can carry a bundle of data to the activity as well. Inside the sendMessage() method, use findViewById() to get the EditText element and add its text value to the intent:
Intent intent = new Intent(this, DisplayMessageActivity.class); EditText editText = (EditText) findViewById(R.id.edit_message); String message = editText.getText().toString(); intent.putExtra(EXTRA_MESSAGE, message);
Note: You now need import statements for android.content.Intent and android.widget.EditText. You'll define the EXTRA_MESSAGE constant in a moment.
An Intent can carry a collection of various data types as key-value pairs called extras. The putExtra() method takes the key name in the first parameter and the value in the second parameter.
In order for the next activity to query the extra data, you should define the key for your intent's extra using a public constant. So add the EXTRA_MESSAGE definition to the top of the MainActivity class:
public class MainActivity extends Activity { public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; ... }
It's generally a good practice to define keys for intent extras using your app's package name as a prefix. This ensures they are unique, in case your app interacts with other apps.