Android Custom Event Listener

68,505

Solution 1

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.
    }
 });   

Solution 2

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
      }
});

Solution 3

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!

Share:
68,505
adarsh
Author by

adarsh

Python + Flask guy

Updated on December 04, 2020

Comments

  • adarsh
    adarsh over 3 years

    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?

  • Jacob Wan
    Jacob Wan about 11 years
    Are the parentheses on the OnCustomEventListener interface definition a typo?
  • Brabbeldas
    Brabbeldas about 9 years
    rDroid, thanks for the example! Should the OnCustomEventListener interface be declared inside or outside the CustomView class?
  • rDroid
    rDroid about 9 years
    usually interface is declared inside the view classes.
  • Admin
    Admin about 8 years
    Sorry for asking late but I didnt get the purpose of setCustomEventListener, you havent called this function anywhere in the code
  • hjchin
    hjchin about 8 years
    @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.
  • Steven
    Steven about 8 years
    Thanks. Very helpful!
  • Ramkesh Yadav
    Ramkesh Yadav over 4 years
    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 } });