Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.os.Handler;
import android.os.SystemClock;

import android.view.MotionEvent;
import android.view.View;

public class Main {
    public static void dispatchTouch(View view) {
        dispatchTouch(view, 200);
    }

    public static void dispatchTouch(final View view, final long duration) {
        final long downTime = SystemClock.uptimeMillis();
        final long eventTime = SystemClock.uptimeMillis();
        final float x = view.getWidth() / 3;//0.0f;
        final float y = view.getHeight() / 3;//0.0f;
        // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
        final int metaState = 0;
        MotionEvent motionEvent = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, metaState);

        // Dispatch touch event to view
        view.dispatchTouchEvent(motionEvent);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                MotionEvent motionEvent = MotionEvent.obtain(downTime + duration, eventTime + duration,
                        MotionEvent.ACTION_UP, x, y, metaState);
                view.dispatchTouchEvent(motionEvent);
            }
        }, duration);
    }
}