ParagonTime
ParagonTime

Reputation: 145

How to use if statements with gestures sequentially in Android Studio?

I'm trying to setup a way to only accept one gesture at a time then after that gesture is done do the next in line. So for example I want to start with a single tap then after you input the tap it would move on to the next one being a double tap. Here's the code I'm using:

import android.content.DialogInterface;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.TextView;

public class secondscreen extends ActionBarActivity implements GestureDetector.OnGestureListener,
        GestureDetector.OnDoubleTapListener{


    private TextView mainMessage;                      //variable refers to strings.xml//    private TextView InText;
    private TextView startButton;

    private GestureDetectorCompat gestureDetector;     // Secondary Variable used for gestures
    private DialogInterface.OnClickListener OnClickListener;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secondscreen2);

        mainMessage = (TextView) findViewById(R.id.mainMessage);



        this.gestureDetector = new GestureDetectorCompat(this, this);
        gestureDetector.setOnDoubleTapListener(this);


    }

    ///////// GESTURE METHODS //////////

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        mainMessage.setText("single tap");


        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        mainMessage.setText("double tap");

        return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        mainMessage.setText("double tapping event");

        return false;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        mainMessage.setText("down");

        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        mainMessage.setText("showing the press");


    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        mainMessage.setText("tap up");

        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        mainMessage.setText("scrolling is fun");

        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        mainMessage.setText("very long presses");


    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        mainMessage.setText("flinging");

        return false;
    }

    ///////// GESTURE METHODS //////////


    //this next block is needed to run gestures//


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.gestureDetector.onTouchEvent(event);
        return super.onTouchEvent(event);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_secondscreen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

I specifically want the gestures to not happen at the same time as it is set up now, the app will run the gestures and display a text but it does it all at the same time displaying the last one pressed - I want to know how to set it up on one screen so that it asks for a gesture and moves on to the next gesture after that one is pressed preferably waiting a second so it doesn't happen instantly. I'm pretty sure it has something to do with an if else statement but I have no idea how to word it.

Upvotes: 1

Views: 365

Answers (1)

alan7678
alan7678

Reputation: 2311

One simple way I can think of is to just use an enum combined with if statements

enum ex.

private enum EVENT{
    SINGLE_TAP,DOUBLE_TAP,EVENT_3,EVENT_4 ...;
}

private EVENT currentEvent=EVENT.SINGLE_TAP;

Event Handler

@Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        if (currentEvent==Event.SINGLE_TAP){
            mainMessage.setText("single tap");
            currentEvent=Event.DOUBLE_TAP;//set to next desired event
            return true;
        }

        return false;
    }

Do something similar in your other event handlers, on the last event i would make it loop back to the first

If you desire a delay between the events implement a stopwatch and check if your desired time has passed between events. Something like so.

@Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
          if(stopWatch.getTime > lastEventTime+delay){//pseudo code
            if (currentEvent==Event.SINGLE_TAP){
                mainMessage.setText("single tap");
                currentEvent=Event.DOUBLE_TAP;//set to next desired event
                return true;
            }
          }

            return false;
        }

Upvotes: 1

Related Questions