Monday, June 5, 2017
Detect user touch with GestureDetectorCompat
Detect user touch with GestureDetectorCompat
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(" - onSingleTapUp -");
return super.onSingleTapUp(e);
}
@Override
public void onLongPress(MotionEvent e) {
textView.append(" - onLongPress -");
super.onLongPress(e);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
textView.append(" - onScroll -");
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
textView.append(" - onFling -");
return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
public void onShowPress(MotionEvent e) {
textView.append(" - onShowPress -");
super.onShowPress(e);
}
@Override
public boolean onDown(MotionEvent e) {
textView.append(" - onDown -");
return super.onDown(e);
}
@Override
public boolean onDoubleTap(MotionEvent e) {
textView.append(" - onDoubleTap -");
return super.onDoubleTap(e);
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
textView.append(" - onDoubleTapEvent -");
return super.onDoubleTapEvent(e);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
textView.append(" - onSingleTapConfirmed -");
return super.onSingleTapConfirmed(e);
}
@Override
public boolean onContextClick(MotionEvent e) {
textView.append(" - onContextClick -");
return super.onContextClick(e);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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>
Labels:
detect,
gesturedetectorcompat,
touch,
user,
with
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.