Detect user touch with GestureDetectorCompat

Advertisemen

Android example to Detect user touch with GestureDetectorCompat:


MainActivity.java
package com.blogspot.android_er.androidgesturedetector;

import android.os.Bundle;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private GestureDetectorCompat gestureDetectorCompat;
TextView textInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textInfo = (TextView)findViewById(R.id.info);

gestureDetectorCompat = new GestureDetectorCompat(
this, new MySimpleOnGestureListener(textInfo));
}

@Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetectorCompat.onTouchEvent(event);
return super.onTouchEvent(event);
}

private class MySimpleOnGestureListener
extends GestureDetector.SimpleOnGestureListener{

TextView textView;


public MySimpleOnGestureListener(TextView v) {
super();
textView = v;
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
textView.append("\n- onSingleTapUp -");
return super.onSingleTapUp(e);
}

@Override
public void onLongPress(MotionEvent e) {
textView.append("\n- onLongPress -");
super.onLongPress(e);
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
textView.append("\n- onScroll -");
return super.onScroll(e1, e2, distanceX, distanceY);
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
textView.append("\n- onFling -");
return super.onFling(e1, e2, velocityX, velocityY);
}

@Override
public void onShowPress(MotionEvent e) {
textView.append("\n- onShowPress -");
super.onShowPress(e);
}

@Override
public boolean onDown(MotionEvent e) {
textView.append("\n- onDown -");
return super.onDown(e);
}

@Override
public boolean onDoubleTap(MotionEvent e) {
textView.append("\n- onDoubleTap -");
return super.onDoubleTap(e);
}

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
textView.append("\n- onDoubleTapEvent -");
return super.onDoubleTapEvent(e);
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
textView.append("\n- onSingleTapConfirmed -");
return super.onSingleTapConfirmed(e);
}

@Override
public boolean onContextClick(MotionEvent e) {
textView.append("\n- onContextClick -");
return super.onContextClick(e);
}
}
}


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidgesturedetector.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:textStyle="italic" />
</LinearLayout>


Advertisemen

Disclaimer: Gambar, artikel ataupun video yang ada di web ini terkadang berasal dari berbagai sumber media lain. Hak Cipta sepenuhnya dipegang oleh sumber tersebut. Jika ada masalah terkait hal ini, Anda dapat menghubungi kami disini.
Related Posts
Disqus Comments
© Copyright 2017 GENERAL INFO FOR ANDROID DEVELOPMENT - All Rights Reserved - Template Created by goomsite - Proudly powered by Blogger