Advertisement
yo2man

Android TNB 22. Gestures

Jun 2nd, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. //Android TNB 22. Gestures
  2. package com.thenewboston.gesture;
  3.  
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.widget.TextView;
  9. import android.view.MotionEvent;
  10. import android.view.GestureDetector;
  11. import android.support.v4.view.GestureDetectorCompat;
  12.  
  13.  
  14. public class MainActivity extends ActionBarActivity implements GestureDetector.OnGestureListener,
  15. GestureDetector.OnDoubleTapListener {
  16.  
  17.     private TextView buckysMessage;
  18.     private GestureDetectorCompat gestureDetector;
  19.  
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_main);
  24.  
  25.         buckysMessage = (TextView)findViewById(R.id.buckysMessage);
  26.         this.gestureDetector = new GestureDetectorCompat(this,this);
  27.         gestureDetector.setOnDoubleTapListener(this);
  28.     }
  29.  
  30.     ///////////////////////////Begin Gestures///////////////////////////////////
  31.  
  32.  
  33.     @Override  //You need to Override onTouchEvent. onTouchEvent is the default method called when user touches the screen.
  34.     public boolean onTouchEvent(MotionEvent event) {
  35.         this.gestureDetector.onTouchEvent(event); //we want it to detect if the touch is a gesture first
  36.         return super.onTouchEvent(event);
  37.     }
  38.  
  39.     @Override
  40.     public boolean onSingleTapConfirmed(MotionEvent e) {
  41.         buckysMessage.setText("onSingleTapConfirmed");
  42.         return true;  //return true says: touch event is taken care, event is handled of so it doesn't go back.
  43.     }
  44.  
  45.     @Override
  46.     public boolean onDoubleTap(MotionEvent e) {
  47.         buckysMessage.setText("onDoubleTapConfirmed");
  48.         return true;
  49.     }
  50.  
  51.     @Override
  52.     public boolean onDoubleTapEvent(MotionEvent e) {
  53.         buckysMessage.setText("onDoubleTapEventConfirmed");
  54.         return true;
  55.     }
  56.  
  57.     @Override
  58.     public boolean onDown(MotionEvent e) {
  59.         buckysMessage.setText("onDownConfirmed");
  60.         return true;
  61.     }
  62.  
  63.     @Override
  64.     public void onShowPress(MotionEvent e) {
  65.         buckysMessage.setText("onShowPressConfirmed");
  66.  
  67.     }
  68.  
  69.     @Override
  70.     public boolean onSingleTapUp(MotionEvent e) {
  71.         buckysMessage.setText("onSingleTapUpConfirmed");
  72.         return true;
  73.     }
  74.  
  75.     @Override
  76.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
  77.         buckysMessage.setText("onScrollConfirmed");
  78.         return true;
  79.     }
  80.  
  81.     @Override
  82.     public void onLongPress(MotionEvent e) {
  83.         buckysMessage.setText("onLongPressConfirmed");
  84.  
  85.     }
  86.  
  87.     @Override
  88.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
  89.         buckysMessage.setText("onFlingConfirmed");
  90.         return true;
  91.     }
  92.  
  93.     ///////////////////////////End Gestures///////////////////////////////////
  94.     @Override
  95.     public boolean onCreateOptionsMenu(Menu menu) {
  96.         // Inflate the menu; this adds items to the action bar if it is present.
  97.         getMenuInflater().inflate(R.menu.menu_main, menu);
  98.         return true;
  99.     }
  100.  
  101.     @Override
  102.     public boolean onOptionsItemSelected(MenuItem item) {
  103.         // Handle action bar item clicks here. The action bar will
  104.         // automatically handle clicks on the Home/Up button, so long
  105.         // as you specify a parent activity in AndroidManifest.xml.
  106.         int id = item.getItemId();
  107.  
  108.         //noinspection SimplifiableIfStatement
  109.         if (id == R.id.action_settings) {
  110.             return true;
  111.         }
  112.  
  113.         return super.onOptionsItemSelected(item);
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement