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 theView
that was clicked)