CHAPTER 7: Making Apps Interactive: Intents, Event Handling, and Menus 251
android:layout_below="@+id/edit_fleet" android:layout_alignLeft="@+id/edit_colony"
android:layout_alignBaseline="@+id/submit_ships" android:textColorHint="#FFFFFF"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button android:id="@+id/submit_ships" android:text="@string/ok_button"
android:layout_toRightOf="@+id/edit_ships" android:textColor="#FFFFFF"
android:layout_below="@+id/submit_fleet" android:layout_alignLeft="@+id/submit_colony"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
Now you are ready to make the Edit Galaxy Info UI screen interactive using event listeners and event
handling!
Event Handling: Using Event Listeners
Android uses something called an “event” to allow the end users of your application to “interact”
with your UI design. There are different events for button (or mouse) clicks, touchscreen use, and
keyboard use, and your Java code must “handle” these events by implementing “event listener”
code structures that “trap” these events as they move through your code. When a user “interfaces”
with their Android device hardware, this will generate one of these events, and the Android OS will
deliver this event to your Java code for processing, using your “event handler” Java code.
Creating an Event Listener: .onClickListener( )
First, let’s add Button and EditText object declarations at the top of the EditGalaxy.java class, as
shown in Figure 7-34.
Figure 7-34. Add EditText and Button object declaration at the top of the class, mouse-over each, and Import each class