tags:

views:

68

answers:

3

Hi all,

Wondering if there is any way to build and fire an event (e.g. on click event) in android applications programmatically.

Thanks.

sorry, it seems question is not clear enough, let me explain a bit more:

  • I have an Activity (lets call it A) with multiple views (5 ImageView for example).
  • And a normal Java class (lets call it B) which is used by my Activity.
  • There is an instance of B in my Activity.
  • If user click on a View (Image View) the view OnClickListener calls a method in B
  • In B, if operation is successful, it will call back a method in activity again.
  • in activity method, It will change image or state for clicked ImageView.

in the other hand:

click on view (x) in Activity -------> B.doSomething() --------> A.bIsDone() -----> change image for view (x)

with normal execution, its working and there is no problem.

the problem is that I want to simulate this process to be automatic, my code looks like this:

        while (!b.isFinished()) {
            try {
                b.doSomething(<for View x>);
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }           

The issue is that, it won't render anything, until the end of the loop.

I tried to put the loop in another Thread, but its throwing exception:

 android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

what can i do for this issue? i was think generate an click event on view (by code), to see it can help or not.

what is your solution? Thanks

+2  A: 
st0le
my question was about building and firing event, not handling them.
mohammad shamsi
please explain the downvote...
st0le
@mohammad, what are you talking about? `onKeyUp, onKeyDown` handles messages, `dispatchKeyEvent` is used to fire an event. Maybe you should word your problem better.
st0le
@mohammad, `View.performClick` can simulate a click. If a click is all your looking for.
st0le
@mohammad, now, that your desciption is better, i've updated my answer accordingly.
st0le
@StOle, thanks, runOnUiThread solved the issue.
mohammad shamsi
A: 

I'm not sure what you mean by build and fire an event and on which context (i.e. from a View, Activity or Service) but will give it a try.

To create a key event just use the KeyEvent Contructors.

For MotionEvents you can use the static obtain method from [here][1]

If you want to fire them use the previously mentioned methods on the target Activity (which can be fetch as the context (getContext()) if inside of a View)

Hope it helps, if it doesn't please provide with more details and/or example code.

[1]: http://developer.android.com/reference/android/view/MotionEvent.html#obtain(long, long, int, float, float, int)

pablisco
A: 

If you are talking about Listeners, like OnClickListener, then yes: You can create your own listeners and trigger them.

Basically you start by defining an interface for that Listener. For a DaterTimePicker (which has a DaterPicker and TimerPicker) widget I once used

public interface OnDateTimeSetListener { public abstract void onDateTimeSet(DatePicker datePicker, TimePicker timePicker); }

The interface defines a single Method which has to be implemented by your listeners.

Then in your class you do something like

public class DateTimePickerDialog extends AlertDialog {
    private OnDateTimeSetListener onDateTimeSetListener;
    private DatePicker datePicker;
    private TimePicker timePicker;

    public void setOnDateTimeListener(OnDateTimeSetListener l) {
        this.onDateTimeSetListener = l;
    }

    private onDateTimeSet() {
        if(onDateTimeSetListener!=null)
            onDateTimeSetListener.onDateTimeSet(this.datePicker, this.timePicker);
    }

    private doSomething() {
        // Do your code here 

        // fire up the event once finished
        onDateTimeSet();
    }
}

setOnDateTimeListener() is used to set listeners from outside of the class (i.e. in your main activity).

onDateTimeSet is used internally (hence declared private) to fire the event and to check if onDateTimeSetListener was set or else we'd get a NullPointerException if it wasn't set. If it was set, call it's onDateTimeSet method.

And in your main Activity you simply add an listener to it and add the code you need, like:

DateTimePicker dateTimePicker = (DateTimePicker)findViewById(R.id.datetime);
dateTimePicker.setOnDateTimeListener(new OnDateTimeSetListener () {
    void onDateTimeSet(DatePicker datePicker, TimePicker timePicker) {
        // Date/Time was changed
        Log.d("OnDateTimeSet", "Date/time was updated");
    }
});

Here you set up the listener the same way you'd set up an OnClickListener

It's quite a bit of code for a you have to do for a simple event, but as far as I know it's the right way to implement this

Tseng