This is a continuation from the previous post.
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:
This requires that you import the View class:
Build an Intent
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.
sendMessage() method, create an
Intent to start an activity called
An intent not only allows you to start another activity, but it can carry a bundle of data to the activity as well. Inside thesendMessage() method, use findViewById() to get theEditText element and add its text value to the intent:
Start the Second Activity
With this new code, the complete
sendMessage() method that's invoked by the Send button now looks like this:
To create a new activity using Eclipse:
2. In the window that appears, open theAndroid folder and select Android Activity. Click Next.
3. Select BlankActivity and click Next.
4. Fill in the activity details:
o Project: MyFirstApp
o Activity Name: DisplayMessageActivity
o Layout Name: activity_display_message
o Title: My Message
o Hierarchial Parent: com.example.myfirstapp.MainActivity
o Navigation Type: None
If you're using a different IDE or the command line tools, create a new file namedDisplayMessageActivity.java in the project's src/ directory, next to the original MainActivity.java file.
Open the DisplayMessageActivity.java file. If you used Eclipse to create this activity:
· The class already includes an implementation of the required onCreate() method.
· There's also an implementation of the onCreateOptionsMenu() method, but you won't need it for this app so you can remove it.
· There's also an implementation of onOptionsItemSelected() which handles the behavior for the action bar's Up behavior. Keep this one the way it is.
Because the ActionBar APIs are available only on HONEYCOMB (API level 11) and higher, you must add a condition around the getActionBar() method to check the current platform version. Additionally, you must add the @SuppressLint("NewApi") tag to the onCreate() method to avoid lint errors.
The DisplayMessageActivity class should now look like this:
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
Add it to the manifest
All activities must be declared in your manifest file,
AndroidManifest.xml, using an
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:
Receive the Intent
onCreate() method, get the intent and extract the message delivered by
Display the Message
onCreate() method for
DisplayMessageActivity now looks like this:
The final set of code that you would have arrived could be the following
This enabled me to do the following J