Example usage for android.view View onTouchEvent

List of usage examples for android.view View onTouchEvent

Introduction

In this page you can find the example usage for android.view View onTouchEvent.

Prototype

public boolean onTouchEvent(MotionEvent event) 

Source Link

Document

Implement this method to handle touch screen motion events.

Usage

From source file:Main.java

private static void sendAction(View view, int action, long downTime, float x, float y) {
    long eventTime = SystemClock.uptimeMillis();
    MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, 0);
    view.onTouchEvent(event);
}

From source file:Main.java

/**
 * Performs a single touch on the center of the supplied view.
 * This is safe to call from the instrumentation thread and will invoke the touch
 * asynchronously./*from w  w w.  j ava 2 s  . com*/
 *
 * @param view The view the coordinates are relative to.
 */
public static void simulateTouchCenterOfView(final View view) throws Throwable {
    view.post(new Runnable() {
        @Override
        public void run() {
            long eventTime = SystemClock.uptimeMillis();
            float x = (float) (view.getRight() - view.getLeft()) / 2;
            float y = (float) (view.getBottom() - view.getTop()) / 2;
            view.onTouchEvent(MotionEvent.obtain(eventTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0));
            view.onTouchEvent(MotionEvent.obtain(eventTime, eventTime, MotionEvent.ACTION_UP, x, y, 0));
        }
    });
}

From source file:Main.java

public static void makeListViewWorkableInScollView(ListView lv) {
    lv.setOnTouchListener(new ListView.OnTouchListener() {

        @Override/* www  .ja va  2s  . c om*/
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                // Disallow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;

            case MotionEvent.ACTION_UP:
                // Allow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }

            // Handle ListView touch events.
            v.onTouchEvent(event);
            return true;
        }
    });
}

From source file:com.handpoint.headstart.client.ui.PasscodeFragment.java

@Override
public boolean onTouch(View v, MotionEvent event) {
    v.onTouchEvent(event);
    if (event.getAction() == MotionEvent.ACTION_UP) {
        if (mPassword1View.getText().length() == 0) {
            mPassword1View.requestFocus();
        } else if (mPassword2View.getText().length() == 0) {
            mPassword2View.requestFocus();
        } else if (mPassword3View.getText().length() == 0) {
            mPassword3View.requestFocus();
        } else {/*  w w  w . j av  a 2s .  co m*/
            mPassword4View.requestFocus();
        }
        return true;
    }
    return false;
}

From source file:com.tlongdev.bktf.ui.fragment.ConverterFragment.java

/**
 * This disables the soft keyboard because we don't need it
 *//*  ww w  .j  a  v  a  2  s .c  o  m*/
@OnTouch({ R.id.edit_text_earbuds, R.id.edit_text_keys, R.id.edit_text_metal, R.id.edit_text_usd })
public boolean onTouch(View v, MotionEvent event) {
    v.onTouchEvent(event);
    Utility.hideKeyboard(getActivity());
    return true;
}

From source file:uk.org.downiesoft.slideshow.SlideShowActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Window win = getWindow();//from  ww w  . j  a  v  a  2 s. c o m
    win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    win.requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    mUiHider = new UiHider(this);
    getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(mUiHider);
    mCacheManager = ThumbnailCacheManager.getInstance(this);
    SlideShowActivity.debug(1, TAG, "onCreate: %s",
            savedInstanceState == null ? "null" : savedInstanceState.toString());
    setContentView(R.layout.activity_slide_show);
    PreferenceManager.setDefaultValues(this, R.xml.view_preferences, false);
    PreferenceManager.setDefaultValues(this, R.xml.slideshow_preferences, false);
    PreferenceManager.setDefaultValues(this, R.xml.cache_preferences, false);
    PreferenceManager.setDefaultValues(this, R.xml.wallpaper_preferences, false);
    PreferenceManager.setDefaultValues(this, R.xml.other_preferences, false);
    mSlideshowSettings = PreferenceManager.getDefaultSharedPreferences(this);
    int thumbSize = Integer.parseInt(mSlideshowSettings.getString(getString(R.string.PREFS_THUMBSIZE), "1"));
    // Hack to convert old hard-coded thumbsize settings to platform dependent sizes
    if (thumbSize > 2) {
        thumbSize = thumbSize / 90;
        SharedPreferences.Editor editor = mSlideshowSettings.edit();
        editor.putString(getString(R.string.PREFS_THUMBSIZE), Integer.toString(thumbSize));
        editor.apply();
    }
    FragmentManager fm = getFragmentManager();
    int cacheLimit = Integer
            .parseInt(mSlideshowSettings.getString(getString(R.string.PREFS_CACHE_LIMIT), "10"));
    mPreviewButton = (ImageView) findViewById(R.id.slideShowImageButton);
    mPreviewView = (FrameLayout) findViewById(R.id.previewContainer);
    RelativeLayout divider = (RelativeLayout) findViewById(R.id.frameDivider);
    if (divider != null) {
        divider.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                return mGestureDetector.onTouchEvent(event) || view.onTouchEvent(event);
            }
        });
    }
    mPreviewFragment = (PreviewFragment) fm.findFragmentByTag(PreviewFragment.TAG);
    if (mPreviewView != null && mPreviewFragment == null) {
        mPreviewFragment = new PreviewFragment();
        fm.beginTransaction().replace(R.id.previewContainer, mPreviewFragment, PreviewFragment.TAG).commit();
    }
    mGridViewFragment = (GridViewFragment) fm.findFragmentByTag(GridViewFragment.TAG);
    if (mGridViewFragment == null) {
        mGridViewFragment = new GridViewFragment();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragmentContainer, mGridViewFragment, GridViewFragment.TAG);
        ft.commit();
    }
    mGestureDetector = new GestureDetector(this, new DividerGestureListener());
    // restart/stop service as required
    Intent intent = getIntent();
    if (intent != null && intent.getAction() != null && intent.getAction().equals(ACTION_STOP_WALLPAPER)
            && isServiceRunning()) {
        stopWallpaperService();
        finish();
    } else {
        if (!mSlideshowSettings.getBoolean(getString(R.string.PREFS_WALLPAPER_ENABLE), false)) {
            if (isServiceRunning()) {
                stopWallpaperService();
            }
        } else {
            if (!isServiceRunning()) {
                Intent startIntent = new Intent(SlideShowActivity.this, WallpaperService.class);
                startService(startIntent);
                invalidateOptionsMenu();
            }
        }
    }
    mCacheManager.tidyCache(cacheLimit);
    BitmapManager.setDisplayMetrics(getResources().getDisplayMetrics());
}

From source file:org.dalol.orthodoxmezmurmedia.utilities.widgets.AmharicKeyboardView.java

private void initializeEditText() {
    mEditText.setOnTouchListener(new OnTouchListener() {

        @Override/* w ww.j a  va  2 s.co  m*/
        public boolean onTouch(View v, MotionEvent event) {
            v.onTouchEvent(event);
            KeyboardUtils.hideSoftKeyboard(mActivity, v);
            showCustomKeyboard();
            return true;
        }
    });

    mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                showCustomKeyboard();
            } else {
                hideCustomKeyboard();
            }
        }
    });
}

From source file:com.google.android.gms.samples.vision.barcodereader.BarcodeCapture.java

/**
 * Initializes the UI and creates the detector pipeline.
 *///from  ww  w . j a  v  a 2  s  .c o  m

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.barcode_capture, container, false);

    mPreview = (CameraSourcePreview) rootView.findViewById(R.id.preview);
    mGraphicOverlay = (GraphicOverlay<BarcodeGraphic>) rootView.findViewById(R.id.graphicOverlay);
    mGraphicOverlay.setShowText(isShouldShowText());
    mGraphicOverlay.setRectColors(getRectColors());
    mGraphicOverlay.setDrawRect(isShowDrawRect());

    // read parameters from the intent used to launch the activity.

    requestCameraPermission();

    gestureDetector = new GestureDetector(getContext(), new CaptureGestureListener());
    scaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleListener());

    rootView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent e) {
            boolean b = scaleGestureDetector.onTouchEvent(e);

            boolean c = gestureDetector.onTouchEvent(e);
            return b || c || view.onTouchEvent(e);
        }
    });
    return rootView;
}

From source file:de.lmu.ifi.medien.probui.system.ProbUIManager.java

public void manageHelper(MotionEvent ev) throws WrongObservationDelegationException {

    this.currentTouchObservations.clear();

    int action = MotionEventCompat.getActionMasked(ev);
    int index = MotionEventCompat.getActionIndex(ev);
    int pointerID = ev.getPointerId(index);

    int type = -1;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN:
        type = ProbObservationTouch.TYPE_TOUCH_DOWN;
        break;/*from   w  ww. j a  va 2 s.co  m*/
    case MotionEvent.ACTION_MOVE:
        type = ProbObservationTouch.TYPE_TOUCH_MOVE;
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
        type = ProbObservationTouch.TYPE_TOUCH_UP;
        break;
    default:
        type = -1;
        break;
    }

    long timestamp = ev.getEventTime();

    ProbObservationTouch observation = ProbObservationFactory.createTouchObservation(ev.getX(index),
            ev.getY(index), ev.getX(index) * 1.0 / container.getWidth(),
            ev.getY(index) * 1.0 / container.getHeight(), ev.getOrientation(index),
            ev.getTouchMinor(index) * 1.0 / container.getWidth(),
            ev.getTouchMajor(index) * 1.0 / container.getHeight(), ev.getPressure(index), type, pointerID,
            timestamp);
    this.currentTouchObservations.add(observation);

    // Since move is always associated with the first pointer,
    // we need to manually duplicate it for the second one
    // (TODO: and for further pointers, if we change it to more than 2):
    if (ev.getPointerCount() == 2 && type == ProbObservationTouch.TYPE_TOUCH_MOVE) {
        ProbObservationTouch observation2 = ProbObservationFactory.createTouchObservation(ev.getX(index),
                ev.getY(index), ev.getX(1) * 1.0 / container.getWidth(),
                ev.getY(1) * 1.0 / container.getHeight(), ev.getOrientation(1),
                ev.getToolMinor(1) * 1.0 / container.getWidth(),
                ev.getToolMajor(1) * 1.0 / container.getHeight(), ev.getPressure(1), type, ev.getPointerId(1),
                timestamp);
        this.currentTouchObservations.add(observation2);
    }

    //Log.d("MULTITOUCH", "type: " + type + ", index: " + pointerID + ", size: " + ev.getTouchMajor(index) * 1.0 / container.getHeight());

    // Distribute touch observation to the cores of all probInteractors
    // (for reasoning by these interactor cores!, not for visual feedback etc. - that comes below: interactor.onTouchDown etc.)
    boolean passedOn = false;
    for (ProbInteractor interactor : this.probInteractors) {

        for (int i = 0; i < this.currentTouchObservations.size(); i++) {
            ProbObservationTouch obs = this.currentTouchObservations.get(i);
            if (obs == null)
                continue;
            if (obs.getNominalFeatures()[0] != ProbObservationTouch.TYPE_TOUCH_MOVE
                    || this.currentTouchObservations.size() != this.previousTouchObservations.size()) {
                interactor.getCore().onTouchObservation(obs);
                passedOn = true;
            } else { // This code filters out move events that moved very little (potentially improves performance):
                double[] obsXY = this.currentTouchObservations.get(i).getRealFeatures();
                double[] obsPrevXY = this.previousTouchObservations.get(i).getRealFeatures();
                double dx = obsXY[0] - obsPrevXY[0];
                double dy = obsXY[1] - obsPrevXY[1];
                double dist = Math.sqrt(dx * dx + dy * dy);
                if (dist > 0.0125) { // TODO: movement threshold currently hardcoded: 0.0125
                    interactor.getCore().onTouchObservation(obs);
                    passedOn = true;
                } else {
                }
            }
        }

    }

    if (passedOn) {
        this.previousTouchObservations.clear();
        this.previousTouchObservations.addAll(this.currentTouchObservations);
    }

    // Forward the touch observation for probInteractors
    // to react (e.g. visual feedback, triggering actions, nothing to do with the mediation):
    for (ProbInteractor interactor : this.probInteractors) {
        for (ProbObservationTouch obs : this.currentTouchObservations) {
            if (obs != null) {
                switch (obs.getNominalFeatures()[0]) {

                case ProbObservationTouch.TYPE_TOUCH_DOWN:
                    interactor.onTouchDown(obs);
                    break;

                case ProbObservationTouch.TYPE_TOUCH_MOVE:
                    interactor.onTouchMove(obs);
                    break;

                case ProbObservationTouch.TYPE_TOUCH_UP:
                    interactor.onTouchUp(obs, ev.getPointerCount() - 1);
                    break;
                default:
                    break;
                }
            }
        }
    }

    // If no element is determined yet (i.e. no decision yet), update the reasoning process.
    if (!isOneDetermined() && passedOn) {
        this.mediator.mediate(false);
    }

    // Post mediation: Forward the touch observation again
    // to the post-mediation versions of the onTouch... methods
    for (ProbInteractor interactor : this.probInteractors) {
        for (ProbObservationTouch obs : this.currentTouchObservations) {
            if (obs != null) {
                switch (obs.getNominalFeatures()[0]) {

                case ProbObservationTouch.TYPE_TOUCH_DOWN:
                    interactor.onTouchDownPost(obs);
                    break;

                case ProbObservationTouch.TYPE_TOUCH_MOVE:
                    interactor.onTouchMovePost(obs);
                    break;

                case ProbObservationTouch.TYPE_TOUCH_UP:
                    interactor.onTouchUpPost(obs, ev.getPointerCount() - 1);
                    break;
                default:
                    break;
                }
            }
        }
    }

    // Pass on to other GUI elements:
    if (!isOneDetermined()) {
        for (View view : this.nonProbInteractors) {
            if (view.isFocusable() && view.isEnabled())
                view.onTouchEvent(ev);
        }
    }
}

From source file:com.thelastcrusade.soundstream.components.PlaylistFragment.java

@Override
public void onResume() {
    super.onResume();
    getActivity().setTitle(getTitle());/* w  w w  .  j  ava 2  s .c  o  m*/

    final GestureDetectorCompat songGesture = new GestureDetectorCompat(getActivity(),
            new PlaylistSongGestureListener(getListView()));

    getListView().setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (songGesture.onTouchEvent(event)) {
                if (event.getAction() != MotionEvent.ACTION_DOWN) {
                    MotionEvent cancelEvent = MotionEvent.obtain(event);
                    cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
                    v.onTouchEvent(cancelEvent);
                }
                return true;
            }
            return false;
        }
    });
}