Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get listener of a view

I write service that interacts with other apps. It registers listeners on views (buttons, textviews,...), that already have listeners. I need to replace them with my own listeners (works), do some stuff and then unregister my listeners and restore the old ones.

  1. An App with a button with an onClickListener is running
  2. My service registers an onClickListener inside the UI-Thread + do something
  3. My service restores the old listener

It would be easy, if there was a view.getOnClickListener -method. Then I could save the old ones and replace the new listeners when I'm done.

Is there any way to get listeners from a view or have more that one listener of the same type bound to one view?

Button btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            //do something
        }
    });
// I need to do, but found no solution for that.
View.OnClickListener oldListener = btn.getOnClickListener();

If I register the new listeners to a view, the old ones are overridden, right? It would be also okay if both listeners ("new" and "old") exist at the same time. Only the old ones must not be gone.

edit: Unfortunately I have no possibility to save the listener on assignment. I need to get it backwards from the view component.

Thanks

like image 868
Dr. AtZe Avatar asked Jan 30 '13 10:01

Dr. AtZe


People also ask

How can you attach listeners to events in a view?

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.

What is a onClick listener?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.

Which of the following method is used to handle what happens after pressing a button for few seconds?

OnClickListener class in the activity and assign it to an instance that will handle onClick logic in the onCreate activity method. Create 'onClickListener' in the 'onCreate' activity method and assign it to the button using setOnClickListener.

Which class contains the onClick method?

To answer your question directly, the onClickListere is an anonymous class that defines the onClick method, which will handle button events.


2 Answers

Thanks to mihail's hint (thanks for that :)) )with the hidden API, I've found a solution to get a listener back after assignment:

The android.view.View class has a nested class static class ListenerInfo that stores all listeners on a View (API 14+). In older versions the listeners are private fields in the android.view.View.

The field can be accessed with reflection. In my case (API 14+),

// get the nested class `android.view.View$ListenerInfo`
Field listenerInfoField = null;
listenerInfoField = Class.forName("android.view.View").getDeclaredField("mListenerInfo");
if (listenerInfoField != null) {
    listenerInfoField.setAccessible(true);
}
Object myLiObject = null;
myLiObject = listenerInfoField.get(myViewObj);

// get the field mOnClickListener, that holds the listener and cast it to a listener
Field listenerField = null;
listenerField = Class.forName("android.view.View$ListenerInfo").getDeclaredField("mOnClickListener")
if (listenerField != null && myLiObject != null) {
    View.OnClickListener myListener = (View.OnClickListener) listenerField.get(myLiObject);
}

After that code (I missed a lot of try-catch-blocks), the myListener object holds the instance of the onClickListener, that has been anonymously declared to the view before. It also works with any other listener, just replace the "mOnClickListener parameter" with the one you need in the reflection and cast it correctly.

Note that code changes in upcoming versions can make that not working anymore.

Found the final tutorial here: http://andwise.net/?p=161

like image 125
Dr. AtZe Avatar answered Sep 28 '22 00:09

Dr. AtZe


create classes that implements OnClickListener

public static class MyClickListener1 implements OnClickListener{
    Activity mActivity;
    MyClickListener1(Acivity activity){
      mActivity=activity;
    }
    @Override
    public void onClick(View v) {
            //do something
    }
}

public static class MyClickListener2 implements OnClickListener{
    @Override
    public void onClick(View v) {
            //do something
    }
}

and in your code you can easily use them:

btn.setOnClickListener(new MyClickListener1(this));
btn.setOnClickListener(new MyClickListener2());

or you can create instances and reuse them:

OnClickListener listener1 = new MyClickListener1(this);
OnClickListener listener2 = new MyClickListener2();
btn.setOnClickListener(listener1);
btn.setOnClickListener(listener2);

you can also define a constructor to pass whatever you need in these classes. I usually pass the activity like in MyClickListener1

EDIT: If you want to have the listener like object in the button, you can use the tag.

btn.setTag(listener1);
btn.setOnClickListener(listener1);

and then to get it use

OnClickListener old_listener = (OnClickListenr)btn.getTag();
like image 33
mihail Avatar answered Sep 28 '22 01:09

mihail