Mike G
Mike G

Reputation: 4959

Getting a view's listener

Is it possible to get the listener that is attached to a view if it exists ? for example if i have a view i would like to get the listener that is listening to that view if there exists one

Upvotes: 1

Views: 652

Answers (2)

G. Bach
G. Bach

Reputation: 3909

The only method I could find is View.getOnFocusChangeListener(). So a standard-library View will only tell you about its OnFocusChangeListener; for other listeners, no such getters are part of the class, so if you want to introduce those, you may have to subclass View.

Also, there are some methods that tell you whether a specific type of listener is attached to the View without returning the listener, for example whether a drag event listener, an on click listener or an on long click listener is attached to the View. Note: some of these methods may have side effects, check the docs on that.

Upvotes: 1

David Burström
David Burström

Reputation: 1610

No, if you take the OnClickListener for instance, it has protected access in View. Only a subclass could grant public access to the Listener fields, but no framework class does. On the other hand, you have the full responsibility to set the Listener in the first place, so you can set up your own data structures to track that information, if necessary.

Upvotes: 1

Related Questions