Android Event Handlers

Charles Drews
3 min readFeb 11, 2016

--

Any app that doesn’t run solely in the background is going to need to interact with the user. Fortunately, the Android SDK provides a huge variety of methods to help an app recognize when a user does something, and respond appropriately. These are event handlers — a user click, tap, swipe, shake, etc. is an event, and these methods handle those events by giving the programmer a place to add the instructions describing how the app should respond.

Let’s start with a Button. You can add a Button element in your xml layout, and get a corresponding button object in your Java logic like so:

//In your xml layout file:
<Button
android:id="@+id/my_button"
android:text="Click Me!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
//In your Java activity class:
Button myButton = (Button) findViewById(R.id.my_button);

Great, now you have an object in Java that represents the Button appearing on screen in your app! But how do we make it respond to the user’s actions? First thing to consider is what action the user will take — in this case the most likely action is to click the button. That means it’s time for an OnClickListener:

//In your Java activity class, create a new OnClickListener:View.OnClickListener myListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(
MainActivity.this, // put "your activity name".this
"You clicked the button!",
Toast.LENGTH_SHORT
).show();
}
};

This looks complicated at first, but we can break it down. You just created a new object named myListener and it is an instance of the class View.OnClickListener. This means myListener has access to all the methods that Google defined for the OnClickListener class when they added it to their library. One of those methods is named onClick() and as you may have guessed, this method is the one that is triggered by the event of a user’s click.

We could just let myListener use the standard version of onClick() which doesn’t really do anything, or we can override the method to give it a custom definition, just for this particular instance named myListener. In the example above, we did just that, using the @Override decorator followed by a new definition for onClick().

Don’t worry too much about how a Toast works, just know that it shows the app user the specified message (“You clicked the button!”) in a temporary popup whenever that line of code is executed, which is whenever our newly defined onClick() method is called. You should feel free to replace this Toast with any code you like — that code should instruct the app on what you want it to do whenever the button is clicked.

Status check — we added a button in XML, got a reference to it in Java, and instantiated a new OnClickListener containing an overridden onClick() method. How do we connect that listener to the button?

//In your Java activity class, set the listener to the buttonmyButton.setOnClickListener(myListener);

That’s the last step! Now myListener is assigned to myButton and is busy listening for any events that involve the button. If such an event occurs and it is a click event, then myListener does the work for you of calling the onClick() method we defined. That means when a user clicks the button, the chain of events leads to the launching of the Toast message we defined. A direct response to something the user did — interactivity!

There are many kinds of listeners besides OnClickListeners, and many kinds of on event methods besides onClick(). For example, TextViews and EditText views have both OnClickListeners and OnTouchListeners. CheckBox elements have OnCheckedChangeListeners. RatingBar elements (think giving something a rating of 1–5 stars) have OnRatingBarChangeListeners. And so on and so on.

The basic idea for all of them is the same:

  • Add the visual element to your xml file
  • Get a reference to the element in your Java code using findViewById()
  • Think about what user action(s) your app should respond to
  • If need be, do a quick Google search for the listeners available for that element
  • Instantiate a new listener of the necessary type, and override the method that corresponds to the user action you’re targeting
  • Finally, “set” that listener to your Java object that represents your visual element

You now have a basic knowledge of how Android apps respond to user actions. Try to implement some event handlers of your own and send me some responses describing your success and any questions you have!

--

--