Declarative Event Handling
Let's work on a project so we can deep dive into the specifics of event handling. Open the Hello project from the previous chapters, or you can just create another project, that should be fine as well. Whichever you choose, open the layout file (activity_main) from the project tool window and go to design mode.
- select the button
- while it's selected, go to the Attributes inspector and find the onClick attribute
- Type the name of the method (which we will create later) on the attribute field, as shown in Fig 5-2

Any changes on the attribute of the button that was done on the inspector is automatically reflected on the XML layout file.
Listing CH-2 app/src/main/res/layout/activity_main.xml
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="27dp"
android:layout_marginTop="141dp"
android:onClick="sayHello"  (1)
android:tag="mybutton"
android:text="Button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="27dp"
tools:layout_editor_absoluteY="141dp"/>
- This was added automatically by AS3 when you wrote sayHello on the onClick attribute. The update works both ways, you could have edited this XML directly and the update would have been reflected in the attribute inspector. When you run this app, the Android Runtime will look for the function sayHello in MainActivity.java, at the moment, we don't have it yet, so we have to implement that
Filling up the entry of the onClick attribute means you want something to happen when the Button view is clicked, the attribute entry becomes the name of a method which the Android Runtime will look for when a click event happens on the Button. The implementation of this method goes in the main program file (MainActivity.java).
To implement the sayHello method, let's open the MainActivity.java in the main editor and write our function. Here's the code snippet for the sayHello method again, as shown in Listing 5-2.
void sayHello(View v) {
System.out.println("Hello");
}
NOTE
When a method is intended to be invoked by an event, it needs to accept a View object as parameter. In our example, when the button is clicked, the Android Runtime invokes the sayHello method and it passes the object reference of whatever view was clicked — this might seem unimportant, even superfluous right now, because we only have one Button object. If we had more, then it will make more sense why accepting a View object is useful
We're ready to test the application. Launch an appropriate AVD so we can see if the application is behaving as intended. Once the AVD is up and running, run the application. You can do from the main menu bar Run → Run app or just click the Run button from the toolbar. Open the Logcat tool window so we can look at the events being logged by Android

Fig 5-3 Logcat Window
To launch the Logcat tool window, click the "Logcat" launcher, it is located somewhere in the bottom left of the AS3 application window, as shown in Fig 5-3. Go to the emulator and click the Button on our app, while you are doing that, try to watch the Logcat window.

Fig 5-4 System.out on Logcat
The output of System.out will not be on the activity, instead, it is redirected to console Logcat window.