Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getOnClickListener() in Android views

Tags:

I need a getOnClickListener() for Views in Android. This way I can assign a temporary OnClickListener to my Views. I want to use it like this:

private View.OnClickListener oldListener;  public void assignTempListener(View view) {     oldListener = view.getOnClickListener(); // doesn't exist      view.setOnClickListener(new View.OnClickListener() {          @Override         public void onClick(View v) {             // some code              v.setOnClickListener(oldListener);         }     }); } 

The problem is that this function doen't exist. I also can't inherit from View to create this method, because all kind of Views can be passed to assignTempListener. Is there another way to use this?

Edit: made a small mistake in my code.

like image 834
user969039 Avatar asked Jun 25 '12 09:06

user969039


People also ask

What is view OnClickListener ()?

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 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.

What does setOnClickListener do in Android?

setOnClickListener(this); means that you want to assign listener for your Button “on this instance” this instance represents OnClickListener and for this reason your class have to implement that interface. If you have more than one button click event, you can use switch case to identify which button is clicked.

What is view V in Android Studio?

The View v is the object of your xml file which is referred in your onCreate method. To refer any component from xml you have to use v to gets its id of the component. Condition. You have give id to the component in xml if you want to use onClick in your class file.


1 Answers

You can do this with...REFLECTION. *drum roll*

This route is fraught with peril.

I don't recommend it, as the internal structure of the class can change at any time, but here's how you can do it currently up to Android 4.2.2 if it's truly unavoidable:

/**  * Returns the current View.OnClickListener for the given View  * @param view the View whose click listener to retrieve  * @return the View.OnClickListener attached to the view; null if it could not be retrieved  */ public View.OnClickListener getOnClickListener(View view) {     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {         return getOnClickListenerV14(view);     } else {         return getOnClickListenerV(view);     } }  //Used for APIs lower than ICS (API 14) private View.OnClickListener getOnClickListenerV(View view) {     View.OnClickListener retrievedListener = null;     String viewStr = "android.view.View";     Field field;      try {         field = Class.forName(viewStr).getDeclaredField("mOnClickListener");         retrievedListener = (View.OnClickListener) field.get(view);     } catch (NoSuchFieldException ex) {         Log.e("Reflection", "No Such Field.");     } catch (IllegalAccessException ex) {         Log.e("Reflection", "Illegal Access.");     } catch (ClassNotFoundException ex) {         Log.e("Reflection", "Class Not Found.");     }      return retrievedListener; }  //Used for new ListenerInfo class structure used beginning with API 14 (ICS) private View.OnClickListener getOnClickListenerV14(View view) {     View.OnClickListener retrievedListener = null;     String viewStr = "android.view.View";     String lInfoStr = "android.view.View$ListenerInfo";      try {         Field listenerField = Class.forName(viewStr).getDeclaredField("mListenerInfo");         Object listenerInfo = null;          if (listenerField != null) {             listenerField.setAccessible(true);             listenerInfo = listenerField.get(view);         }          Field clickListenerField = Class.forName(lInfoStr).getDeclaredField("mOnClickListener");          if (clickListenerField != null && listenerInfo != null) {             retrievedListener = (View.OnClickListener) clickListenerField.get(listenerInfo);         }     } catch (NoSuchFieldException ex) {         Log.e("Reflection", "No Such Field.");     } catch (IllegalAccessException ex) {         Log.e("Reflection", "Illegal Access.");     } catch (ClassNotFoundException ex) {         Log.e("Reflection", "Class Not Found.");     }      return retrievedListener; } 
like image 198
Kevin Coppock Avatar answered Sep 30 '22 11:09

Kevin Coppock