Homework Challenge (22) + Doubts

0 servet portakal · January 5, 2015
MainActivity.java


package com.hotsoft.gesbutchal;

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;
import android.support.v4.view.GestureDetectorCompat;

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


    private TextView upperText;
    private GestureDetectorCompat gestureDetector;

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

        Button centerButton = (Button) findViewById(R.id.centerbutton);
        this.gestureDetector = new GestureDetectorCompat(this, this);
        gestureDetector.setOnDoubleTapListener(this);

        // For the centerButton to do sth on single tap
        centerButton.setOnClickListener(
                new Button.OnClickListener(){
                    public void onClick(View v){

                        upperText = (TextView) findViewById(R.id.uppertext);
                        upperText.setText("I just got clicked");
                    }
                }
        );
    }

    //////////////////////////////////GESTURES BEGIN/////////////////
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        upperText.setText("onSingleTapConfirmed");
        return true;
    }

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

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

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

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

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

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

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

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


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


I've been able to do the task by looking at the codes we've written before. Took some initiative, made mistakes, but been able to fix them.  I feel more confident now.

Having said that, I think I only understand some codes as parts, not every single word in that section of code. And, I try to modify them. Is that normal at this stage?  Do you have any advice for me?

GestureDetectorCompat was highligted red when I chose Lolipop as the lowest API level, and the program didn't compile. IS the method we are using becoming absolute? It works fine when Ice Cream Sandwich is selected as the lowest.

Post a Reply

Replies

Oldest  Newest  Rating
0 Hector Boone · January 6, 2015
Did you select Lolipop for the Minimum SDK when you created your project? Maybe that is the problem.

My project has Minimum SDK API 8 (Froyo) and your code works fine on my emulator running Lolipop.
0 servet portakal · January 6, 2015
Yes, only when Lolipop select for the Minimum SDK, it doesn't work.  Otherwise, it works fine. 

I asked it because I wanted to know if something is changing in Lollipop. 
  • 1

Java / Android Development

103,643 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator