user3139140
user3139140

Reputation: 23

What calls the Android View.OnClickListener API methods?

I have been learning OOP for about 2 to 3 years, but I am still really confused about interfaces.

In Java for example there is an interface called View.OnClickListener. Now I know the class which implements this interface will have to provide body for its abstract methods, but what I do not get is that in the API documentation its written that the onClick method is called when a view is clicked.

So who calls this method? Is there any other class which implements the interface and then somehow call the method when view is clicked?

Link (https://developer.android.com/reference/android/view/View.OnClickListener)

Upvotes: 2

Views: 218

Answers (2)

Stephen C
Stephen C

Reputation: 719689

You are probably a lot less confused about interfaces than you think.

Basically, GUI frameworks like Android's view framework, classic Java Swing, JavaFX, Eclipse SWT and so on are all big and complicated1. But they all work by turning the application "upside down". Instead of your application code controlling everything, the framework is in control. Your application registers a bunch of listeners or "call backs". Then it hans over control to the framework, and waits to be called back to do something.

The GUI framework will have a master "event thread" that reads the stream of low level events coming from the device (clicks and movement from the mouse, or touch screen events, key presses and release on the keyboard, etcetera). For each one, it maps the low level event to the appropriate part of your application, and then calls your application to say something has happened.

Suppose that an Android user taps her finger on the screen. The framework will translate the touch screen event's physical screen position to virtual screen location of some "component" of your app. If that the component was a button, and you had (previously) registered an OnClickListener for that button, the framework would find the button, and call the listener's onclick() method to tell your application "this button has just been clicked".

It is all rather simple and elegant ... and good sound OOP. But the relationship between your code and the framework is upside down to what you would expect.


1 - and too complicated for most people to fully understand. Though in fact, you don't need to fully understand them to use them.

Upvotes: 1

stkent
stkent

Reputation: 20138

Those methods are called by the Android framework itself. This is one of the useful things the framework does on your behalf, so that you don't need to interpret raw user finger input and figure out what constitutes a "click"!

From https://developer.android.com/guide/topics/ui/ui-events (my emphasis):

Within the various View classes that you'll use to compose your layout, you may notice several public callback methods that look useful for UI events. These methods are called by the Android framework when the respective action occurs on that object.

For onClick specifically, that same page says:

This is called when the user either touches the item (when in touch mode), or focuses upon the item with the navigation-keys or trackball and presses the suitable "enter" key or presses down on the trackball.

Upvotes: 1

Related Questions