/images/forum/upload/2014-12-30/735c49976e9ff5b1aeae3f26ac72d526.png

package com.sheffieldkaelin.tutorial21_gestures;

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



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

    private TextView SheffsMessage;
    private GestureDetectorCompat gestureDetector;

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

        // Create Java variable linked to button
        Button myButton = (Button)findViewById(R.id.myButton);

        //Add CLICK listener to myButton and add functionality (change text of SheffsMessage)
        myButton.setOnClickListener(
                new Button.OnClickListener(){
                    public void onClick(View v) {
                        // Response to click
                        TextView SheffsMessage = (TextView)findViewById(R.id.SheffsMessage);
                        SheffsMessage.setText("onClick was called - Button Pressed");
                    }
                }
        );

        myButton.setOnLongClickListener(
                new Button.OnLongClickListener() {
                    public boolean onLongClick(View v) {
                        // Create Java variable linked to SheffsMessage
                        TextView SheffsMessage = (TextView)findViewById(R.id.SheffsMessage);
                        SheffsMessage.setText("onLongClick called - Button Held");
                        return true;
                    }
                }
        );

        // A reference to the message
        SheffsMessage = (TextView)findViewById(R.id.SheffsMessage);
        // A object from this class that allows us to detect gestures.
        this.gestureDetector = new GestureDetectorCompat(this,this);
        // Allows to detect double tabs
        gestureDetector.setOnDoubleTapListener(this);
    }

    /////////////////// Begin Gestures ///////////////////
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        SheffsMessage.setText("onSingleTapConfirmed");
        /*
        This return statement says that this method was handled (true)
            or
        This return statement says that this method was not handled (false)
         */
        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        SheffsMessage.setText("onDoubleTap");
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        SheffsMessage.setText("onDoubleTapEvent");
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        SheffsMessage.setText("onDown");
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        SheffsMessage.setText("onShowPress");

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        SheffsMessage.setText("onSingleTapUp");
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        SheffsMessage.setText("onScroll");
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        SheffsMessage.setText("onLongPress");
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        SheffsMessage.setText("onFling");
        return true;
    }

    /////////////////// Begin Ends ///////////////////


    @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_main, 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);
    }
}