Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current onClickListener of an Android View object

I need to get the current onClickListener --and other kind of listeners-- of a View object in order to install a new proxy-onClickListener which will do some work and then forward the call to the original listener.

Many thanks!!!

like image 664
pedromateo Avatar asked Jul 07 '11 11:07

pedromateo


1 Answers

You just need to be a little creative!

Just do something like this:

Subclass the View.OnClickListener

abstract class ProxyAbleClickListener implements OnClickListener {

    Runnable ProxyAction;

    @Override
    public void onClick(View arg0) {
        if (ProxyAction != null) {
            ProxyAction.run();
        }
    }

    public void setProxyAction(Runnable proxyAction) {
        ProxyAction = proxyAction;
    }
}

/* Somwhere in your code ! */

YourView view = new Button(context);

view.setOnClickListener(new ProxyAbleClickListener() {
    public void onClick(View arg0) {
        // Insert onClick Code here

        super.onClick(arg0);
    }

});

ProxyAbleClickListener listener = (ProxyAbleClickListener) view.getOnClickListener();
listener.setProxyAction(new Runnable() {

    @Override
    public void run() {
        // Insert your proxy code here..

    }
});

Make sure to have a subclassed view Like YourView that overrides the setOnClickListener and keeps a reference to the listener to access with getOnClickListner..

I have not tested this code in any way, treat it as pseudo code for what you need to do.

I hope this will Learn you a few things :)

like image 133
Marhlder Avatar answered Oct 04 '22 21:10

Marhlder