Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Starting Another Activity

Previous Next

This lesson teaches you to

  1. Respond to the Send Button
  2. Build an Intent
  3. Start the Second Activity
  4. Create the Second Activity
  5. Receive the Intent
  6. Display the Message

You should also read

  • 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.


Date: 2014-12-29; view: 856


<== previous page | next page ==>
Make the Input Box Fill in the Screen Width | Sending an intent to other apps
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)