Android Custom Event Listener

android listeners and callbacks
custom listener kotlin
java custom listener
android callback
android interface callback
how to implement listener in java
android custom view click listener
android touch events

Say I want to make my own event listener for my class, how do I do that? Do I need to maintain a thread manually?

public class CustomView extends View(){
OnCustomEventListener mListener;
:
://some code
:
:

Create an interface that will be implemented by your activity:

public interface OnCustomEventListener {
    void onEvent();
}

public void setCustomEventListener(OnCustomEventListener eventListener) {
    mListener = eventListener;
}

Now you need to know when the event is actually occurring. For example when the user touches a point on screen, override onTouchEvent method:

onTouchEvent(MotionEvent ev) {
    if (ev.getAction==MotionEvent.ACTION_DOWN) {
        if(mListener!=null) 
            mListener.onEvent();
    }
}

Similarly, you can create a specific event that you want. (examples could be touch down, wait for exactly 2 seconds and release- you would need to do some logic inside touch event).

In your activity, you can use the customView object to set an eventListener as such:

 customView.setCustomEventListener(new OnCustomEventListener() {
    public void onEvent() {
        //do whatever you want to do when the event is performed.
    }
 });   

Android Custom Event Listener, public class CustomView extends View(){ OnCustomEventListener mListener; : ://​some code : : Create an interface that will be implemented by  Custom Listeners. There are four steps to using a custom listener to manage callbacks in your code: Define an interface as an event contract with methods that define events and arguments which are relevant event data. Setup a listener member variable and setter in the child object which can be assigned an implementation of the interface.

It can be done in the following way

First create an interface class :

public interface OnStopTrackEventListener {
    public void onStopTrack();
}

Then create the class that controls the interface :

public class Player {

    private OnStopTrackEventListener mOnStopTrackEventListener;

    public void setOnStopTrackEventListener(OnStopTrackEventListener eventListener)
    {
        mOnStopTrackEventListener = eventListener;
    }

    public void stop()
    {
        if(mOnStopTrackEventListener != null)
        {
            mOnStopTrackEventListener.onStopTrack();
        }

    }
}

That is all. Let's use it now

Player player = new Player();
player.stop(); //We are stopping music
player.setOnStopTrackEventListener(new OnStopTrackEventListener() {
      @Override
      public void onStopTrack() {
           //Code to work when music stops
      }
});

Creating a Custom Listener in Android (Listener Pattern), The principle of work is based on the fact that in one class we define the event and the moment when the event is started ("trigger the event"),  In android, Input Events are used to capture the events, such as button clicks, edittext touch, etc. from the View objects that defined in a user interface of our application, when the user interacts with it. To handle input events in android, the views must have in place an event listener. The View class, from which all UI components are derived contains a wide range event listener interfaces and each listener interface contains an abstract declaration for a callback method.

I found this tutorial to be VERY HELPFUL. It explains the four steps to using a custom listener to manage callbacks in your code:

1.Define an interface as an event contract with methods that define events and arguments which are relevant event data.

2.Setup a listener member variable and setter in the child object which can be assigned an implementation of the interface.

3.Owner passes in a listener which implements the interface and handles the events from the child object.

4.Trigger events on the defined listener when the object wants to communicate events to it's owner

Hope it helps!

Custom Listeners In Android, They are a very popular way of creating asynchronous callbacks. Listeners are generally used to implement the code that runs when an event occurs. A common​  Create listener. In the next step we need to define an instance variable in the object that will send callback via MyCustomListener. And add setter for our listener. public class SampleClassB { private MyCustomListener listener; public void setMyCustomListener(MyCustomListener listener) { this.listener = listener; } }

Input events overview, android documentation: Custom Listener. Exception e) { //do some stuff if needed //check if listener is set or not. if(listener == null) return; //Fire proper event​. Android Listeners are used to capture events. When, for instance, the user interacts with the Android system by clicking on a button, the Listeners would prompt the underlying activity to do the task associated with the button click. Therefore, whenever you want the Android system to perform a task (when an event occurs), Listeners are at work.

Android, Since Android applications wait for events to be triggered, listeners are by implementing a custom Android listener from that mildly comical,  Any View (Button, TextView, etc) has many event listeners that can be attached using the setOnEvent pattern which involves passing a class that implements a particular event interface. The listeners available to any View include: setOnClickListener - Callback when the view is clicked setOnDragListener - Callback when the view is dragged

The Listener Pattern in Android - mvndy, For the absolute beginners, listeners (AKA callbacks) is a way to intercept user interaction with the app, and with the captured events an app  When the custom event is chosen from the ad mediation flow, the requestBannerAd () method is called on the class name you provided in the settings. The optional parameter defined above is passed

Comments
  • See the CodePath topic Creating Custom Listeners
  • Are the parentheses on the OnCustomEventListener interface definition a typo?
  • rDroid, thanks for the example! Should the OnCustomEventListener interface be declared inside or outside the CustomView class?
  • usually interface is declared inside the view classes.
  • Sorry for asking late but I didnt get the purpose of setCustomEventListener, you havent called this function anywhere in the code
  • @killerprince182 you set CustomEventListener with an anonymous class of type of OnCustomEventListener (an interface) which requires you to implement onEvent function. This anonymous class is assigned to mListener in CustomView. Whenever mListener.onEvent() is triggered, the implemented OnEvent function in Activity will be executed.
  • yes, very simple and understanding code. just required one edit. please use this sequnce to get rid from null pointer exception. Player player = new Player(); player.stop(); //We are stopping music player.setOnStopTrackEventListener(new OnStopTrackEventListener() { @Override public void onStopTrack() { //Code to work when music stops } });