- Define the event listener (And the custom Behavior) for when the event happens.
- Create a new object instance of the event listener (using the constructor).
- Attach The Listener to the view.
understanding of why OnClickListener is an interface with an abstract method called Onclick.
Your role as the developer is to set up the event Listeners for the input events you want to handle your app. More specifically, for each listener you will need to follow three steps on the top. And then l’ll show you the code you need at each step.
First we need to define an event listener class that implements the interface you can call it anything you want as long as it follows the java conventions for naming classes.
In this case we named the class NumberClickListener and this code is located in a new field called NumberClickListener.java when we define the NumberClickListener class the declaration should say public class, followed by the name of the class, followed by the word implements and then the interface name which is OnClickListener in the case so note that implements is a java key word that has a special meaning and you can find a list of these special keywords in the java Oracle documentation here.
We use implements to express that the NumberClickListener class will support the specified interface and provide code for all its abstract methods.
By Implementing the OnClickListener interface for every piece in the Android code that expects an OnClickListener object type we can use the NumberClickListener and right before the method @Override annotation this means that we are overriding the blank method in the interface and providing our own implementation.
Our implementation of the OnClick method is that we want a toast message to pop up that says open the list of numbers We could’ve provided any other customer behavior here.
Once we define our number clickListener class. we need to create an object instance of it. this is what the code looks like in the main activity file. we modify the main activity so that we can immediately start listening for click events as soon as the activity is created.
We’re creating a new NumberClickListener object and storing it in a variable called ClickListener Which is of the variable data type NumberClickListener so once we have an object instance of a NumbersClickListener we need to attach it to the view that we want to listen for clicks on. and this is the code for that.
Assuming we have the view stored as a variable called ButtonView, we just need to call the set OnclickListener method on that view and pass in one parameter argument the ClickListener variable.
However, first we need to find that view object in the viewer hierarchy. so to find the view that shows the number category so here I call the findViewById method and request the view with the ID numbers so once we find the text view we can store on a variable that I’m going to call numbers.
Then we can call the set OnClickListener method on this text view and pass the ClickListener object as input. So here’s how that would look so here we set OnClickListener method on the text view in passing the ClickListener object as it’s input.
So the TextView numbers in our case will store the clickListener object and later if this TextView is pressed the ClickListener jumps into action in order to test that this java code I worte actually works.
I have to delete some old code that we wrote from the button click so in the activity_main XML file l’ll need to delete the Android onClick attribute on the numbers TextView.
If you like this post, don’t forget to share 🙂