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.
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.
To answer your question directly, the onClickListere is an anonymous class that defines the onClick method, which will handle button events.
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.
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.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With