I'm playing with Android and I'd like to know if there's any way to invoke a listener call programmatically, for example, having an onClick()
listener and invoke a call to this listener without touching the screen when the activity is created.
Use the following pattern: public class YourActivity extends Activity implements OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super. onCreate(savedInstanceState); ... // Some initialization here findViewById(R. id.
An event listener is an interface in the View class that contains a single callback method. These methods will be called by the Android framework when the View to which the listener has been registered is triggered by user interaction with the item in the UI.
Advertisements. Events are a useful way to collect data about a user's interaction with interactive components of Applications. Like button presses or screen touch etc. The Android framework maintains an event queue as first-in, first-out (FIFO) basis.
There is no way to get the set OnClickListener
. So you need to store it and call your OnClickListener
directly.
OnClickListener store = new OnClickListener() {/*...*/};
view.setOnClickListener(store);
store.onClick(view);
Never tried that, but after assigning a clickListener
to your object (for example a Button
), call on your onCreate
method myButton.performClick()
.
Android doc :
public boolean performClick ()
Added in API level 1
Call this view's OnClickListener, if it is defined. Performs
all normal actions associated with clicking: reporting accessibility event,
playing a sound, etc.
Returns
True there was an assigned OnClickListener that was called,
false otherwise is returned.
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