Advertisement
yo2man

Android TNB 21. Button Text Gesture Practice

Jun 3rd, 2015
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.76 KB | None | 0 0
  1. Create a app that has a button and a text above it.
  2. 1.When you click the button, the text above it changes.
  3. 2.When you scroll, the text above it changes.
  4. //-------------------------------------------------------------------------------------------------------------------------------------
  5. //strings.xml
  6. <resources>
  7.     <string name="app_name">Event Handling: How To Make Your App Interactive</string>
  8.  
  9.     <string name="hello_world">Hello world!</string>
  10.     <string name="action_settings">Settings</string>
  11.     <string name="message_text">Hello</string>
  12.     <string name="button_text">Click me Hoss</string>
  13. </resources>
  14.  
  15.  
  16. //-------------------------------------------------------------------------------------------------------------------------------------
  17.  
  18. package com.thenewboston.myapplicationpracticebuttongestures;
  19.  
  20. import android.support.v4.view.GestureDetectorCompat;
  21. import android.support.v7.app.ActionBarActivity;
  22. import android.os.Bundle;
  23. import android.view.GestureDetector;
  24. import android.view.Menu;
  25. import android.view.MenuItem;
  26. import android.view.MotionEvent;
  27. import android.view.View;
  28. import android.widget.Button;
  29. import android.widget.TextView;
  30.  
  31.  
  32. public class MainActivity extends ActionBarActivity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {
  33.  
  34.     private TextView helloText;
  35.     private GestureDetectorCompat gestureDetector;
  36.  
  37.  
  38.     @Override
  39.     protected void onCreate(Bundle savedInstanceState) {
  40.         super.onCreate(savedInstanceState);
  41.         setContentView(R.layout.activity_main);
  42.  
  43.         Button button = (Button)findViewById(R.id.button);
  44.  
  45.         button.setOnClickListener(
  46.                 new Button.OnClickListener() {
  47.                     public void onClick(View v) {
  48.                         TextView helloText = (TextView) findViewById(R.id.helloText);
  49.                         helloText.setText("Good Job YoYo!");
  50.                     }
  51.                 }
  52.         );
  53.  
  54.         button.setOnLongClickListener(
  55.                 new Button.OnLongClickListener() {
  56.                     public boolean onLongClick(View v) {
  57.                         TextView helloText = (TextView) findViewById(R.id.helloText);
  58.                         helloText.setText("That was a long press");
  59.                         return true; //whenever we have an OnLongClick we need a "return true"
  60.                     }
  61.                 }
  62.  
  63.         );
  64.  
  65.  
  66.  
  67.         helloText = (TextView)findViewById(R.id.helloText);
  68.         this.gestureDetector = new GestureDetectorCompat(this,this);
  69.         gestureDetector.setOnDoubleTapListener(this);
  70.     }
  71.  
  72.     ///////////////////////////Begin Gestures///////////////////////////////////
  73.  
  74.  
  75.     @Override  //You need to Override onTouchEvent. onTouchEvent is the default method called when user touches the screen.
  76.     public boolean onTouchEvent(MotionEvent event) {
  77.         this.gestureDetector.onTouchEvent(event); //we want it to detect if the touch is a gesture first
  78.         return super.onTouchEvent(event);
  79.     }
  80.  
  81.     @Override
  82.     public boolean onDown(MotionEvent e) {
  83.         return false;
  84.     }
  85.  
  86.     @Override
  87.     public void onShowPress(MotionEvent e) {
  88.  
  89.     }
  90.  
  91.     @Override
  92.     public boolean onSingleTapUp(MotionEvent e) {
  93.         return false;
  94.     }
  95.  
  96.     @Override
  97.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
  98.         helloText.setText("Scrolling...");
  99.         return true;
  100.     }
  101.  
  102.     @Override
  103.     public void onLongPress(MotionEvent e) {
  104.  
  105.     }
  106.  
  107.     @Override
  108.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
  109.         return false;
  110.     }
  111.  
  112.     ///////////////////////////End Gestures///////////////////////////////////
  113.  
  114.     @Override
  115.     public boolean onCreateOptionsMenu(Menu menu) {
  116.         // Inflate the menu; this adds items to the action bar if it is present.
  117.         getMenuInflater().inflate(R.menu.menu_main, menu);
  118.         return true;
  119.     }
  120.  
  121.     @Override
  122.     public boolean onOptionsItemSelected(MenuItem item) {
  123.         // Handle action bar item clicks here. The action bar will
  124.         // automatically handle clicks on the Home/Up button, so long
  125.         // as you specify a parent activity in AndroidManifest.xml.
  126.         int id = item.getItemId();
  127.  
  128.         //noinspection SimplifiableIfStatement
  129.         if (id == R.id.action_settings) {
  130.             return true;
  131.         }
  132.  
  133.         return super.onOptionsItemSelected(item);
  134.     }
  135.  
  136.     @Override
  137.     public boolean onSingleTapConfirmed(MotionEvent e) {
  138.         return false;
  139.     }
  140.  
  141.     @Override
  142.     public boolean onDoubleTap(MotionEvent e) {
  143.         return false;
  144.     }
  145.  
  146.     @Override
  147.     public boolean onDoubleTapEvent(MotionEvent e) {
  148.         return false;
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement