Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke a listener call programmatically in Android

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.

like image 535
user2281682 Avatar asked Apr 15 '13 08:04

user2281682


People also ask

How do you call a listener on Android?

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.

How listener works in Android?

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.

What is event handling in Android?

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.


2 Answers

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);
like image 140
rekire Avatar answered Oct 30 '22 23:10

rekire


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.
like image 30
Alexis C. Avatar answered Oct 30 '22 23:10

Alexis C.