qwisatz
Lurker
I have a button that has onTouchListener that uses android_gesture_detector.
I'm trying to get something simple working with the gesture detector.
The whole xml basically has a textView called "topText" on it.
I gave the textView a listener so "topText.setOnTouchListener".
The conditions I've setted are
1. If single tap then topText.setText("tap Press")
2. else if onScroll then topText.setText("scroll Press")
3. else if doubleTap then topText.setText("Double Tap")
just some simple conditions to test if it works.
In the Logcat, it detects all the touch gestures. The problem is it only runs the first condition from my if statement in my "topText.setOnTouchListener" so the app on does "tap Press" and double tapping the textView doesn't change the text to "Double Tap". How do I enable the other conditions to work? It ignores all my other else if statements.
My full Main Activity Code:
My XML layout:
I'm trying to get something simple working with the gesture detector.
The whole xml basically has a textView called "topText" on it.
I gave the textView a listener so "topText.setOnTouchListener".
The conditions I've setted are
1. If single tap then topText.setText("tap Press")
2. else if onScroll then topText.setText("scroll Press")
3. else if doubleTap then topText.setText("Double Tap")
Code:
topText.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View view, MotionEvent e) {
//IF SHORT PRESS
if(android_gesture_detector.onSingleTapConfirmed(e)){
topText.setText("TAP PRESS");
} else if (android_gesture_detector.onScroll(e, e, 100 , 50)){
//ON SCROLL TOUCH
topText.setText("THIS IS A DRAG");
} else if (android_gesture_detector.onDoubleTap(e)){
//ON DOUBLE TAP
topText.setText("THIS IS A DOUBLE TAP");
}
return false;
}
});
just some simple conditions to test if it works.
In the Logcat, it detects all the touch gestures. The problem is it only runs the first condition from my if statement in my "topText.setOnTouchListener" so the app on does "tap Press" and double tapping the textView doesn't change the text to "Double Tap". How do I enable the other conditions to work? It ignores all my other else if statements.
My full Main Activity Code:
Code:
package com.example.qwisatz.androidgestures;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//GestureDetector OBJECT
private GestureDetector mGestureDetector;
TextView topText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
topText = (TextView) findViewById(R.id.topText);
button = (Button) findViewById(R.id.button);
// Create an object of the Android_Gesture_Detector Class
final Android_Gesture_Detector android_gesture_detector = new Android_Gesture_Detector();
// Create a GestureDetector
mGestureDetector = new GestureDetector(this, android_gesture_detector); //android_gesture_detector = GestureDetector.OnGestureListener
topText.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View view, MotionEvent e) {
//IF SHORT PRESS
if(android_gesture_detector.onSingleTapConfirmed(e)){
topText.setText("TAP PRESS");
} else if (android_gesture_detector.onScroll(e, e, 100 , 50)){
//ON SCROLL TOUCH
topText.setText("THIS IS A DRAG");
} else if (android_gesture_detector.onDoubleTap(e)){
//ON DOUBLE TAP
topText.setText("THIS IS A DOUBLE TAP");
}
return false;
}
});
}
//INTERCEPT ALL BASIC GESTURES IMPLEMENTED IN IT
//REROUTES THEM TO THE Android_Gesture_Detector CLASS
@Override
public boolean onTouchEvent(MotionEvent event) {
mGestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
// Return true if you have consumed the event, false if you haven't.
// The default implementation always returns false.
}
}
class Android_Gesture_Detector implements GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener {
//DRAGGING BOOLEAN
@Override
public boolean onDown(MotionEvent e) {
Log.d("Gesture ", " onDown");
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d("Gesture ", " onSingleTapConfirmed");
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.d("Gesture ", " onSingleTapUp");
return true;
}
@Override
public void onShowPress(MotionEvent e) {
Log.d("Gesture ", " onShowPress");
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("Gesture ", " onDoubleTap");
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
Log.d("Gesture ", " onDoubleTapEvent");
return true;
}
@Override
public void onLongPress(MotionEvent e) {
Log.d("Gesture ", " onLongPress");
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d("Gesture ", " onScroll");
if (e1.getY() < e2.getY()){
Log.d("Gesture ", " Scroll Down");
}
if(e1.getY() > e2.getY()){
Log.d("Gesture ", " Scroll Up");
}
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() < e2.getX()) {
Log.d("Gesture ", "Left to Right swipe: "+ e1.getX() + " - " + e2.getX());
Log.d("Speed ", String.valueOf(velocityX) + " pixels/second");
}
if (e1.getX() > e2.getX()) {
Log.d("Gesture ", "Right to Left swipe: "+ e1.getX() + " - " + e2.getX());
Log.d("Speed ", String.valueOf(velocityX) + " pixels/second");
}
if (e1.getY() < e2.getY()) {
Log.d("Gesture ", "Up to Down swipe: " + e1.getX() + " - " + e2.getX());
Log.d("Speed ", String.valueOf(velocityY) + " pixels/second");
}
if (e1.getY() > e2.getY()) {
Log.d("Gesture ", "Down to Up swipe: " + e1.getX() + " - " + e2.getX());
Log.d("Speed ", String.valueOf(velocityY) + " pixels/second");
}
return true;
}
}
My XML layout:
Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.qwisatz.androidgestures.MainActivity">
<TextView
android:id="@+id/topText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>