Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Create a app that has a button and a text above it.
- 1.When you click the button, the text above it changes.
- 2.When you scroll, the text above it changes.
- //-------------------------------------------------------------------------------------------------------------------------------------
- //strings.xml
- <resources>
- <string name="app_name">Event Handling: How To Make Your App Interactive</string>
- <string name="hello_world">Hello world!</string>
- <string name="action_settings">Settings</string>
- <string name="message_text">Hello</string>
- <string name="button_text">Click me Hoss</string>
- </resources>
- //-------------------------------------------------------------------------------------------------------------------------------------
- package com.thenewboston.myapplicationpracticebuttongestures;
- 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.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends ActionBarActivity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {
- private TextView helloText;
- private GestureDetectorCompat gestureDetector;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button button = (Button)findViewById(R.id.button);
- button.setOnClickListener(
- new Button.OnClickListener() {
- public void onClick(View v) {
- TextView helloText = (TextView) findViewById(R.id.helloText);
- helloText.setText("Good Job YoYo!");
- }
- }
- );
- button.setOnLongClickListener(
- new Button.OnLongClickListener() {
- public boolean onLongClick(View v) {
- TextView helloText = (TextView) findViewById(R.id.helloText);
- helloText.setText("That was a long press");
- return true; //whenever we have an OnLongClick we need a "return true"
- }
- }
- );
- helloText = (TextView)findViewById(R.id.helloText);
- this.gestureDetector = new GestureDetectorCompat(this,this);
- gestureDetector.setOnDoubleTapListener(this);
- }
- ///////////////////////////Begin Gestures///////////////////////////////////
- @Override //You need to Override onTouchEvent. onTouchEvent is the default method called when user touches the screen.
- public boolean onTouchEvent(MotionEvent event) {
- this.gestureDetector.onTouchEvent(event); //we want it to detect if the touch is a gesture first
- return super.onTouchEvent(event);
- }
- @Override
- public boolean onDown(MotionEvent e) {
- return false;
- }
- @Override
- public void onShowPress(MotionEvent e) {
- }
- @Override
- public boolean onSingleTapUp(MotionEvent e) {
- return false;
- }
- @Override
- public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
- helloText.setText("Scrolling...");
- return true;
- }
- @Override
- public void onLongPress(MotionEvent e) {
- }
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
- return false;
- }
- ///////////////////////////End Gestures///////////////////////////////////
- @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);
- }
- @Override
- public boolean onSingleTapConfirmed(MotionEvent e) {
- return false;
- }
- @Override
- public boolean onDoubleTap(MotionEvent e) {
- return false;
- }
- @Override
- public boolean onDoubleTapEvent(MotionEvent e) {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement