Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android beginner: understanding MotionEvent actions

Tags:

android

I am having trouble getting my activity to generate a MotionEvent.ACTION_UP. Probably a beginner's error.

In LogCat, I'm only seeing the ACTION_MOVE event (which is an int value of 3). I also see the X/Y coordinates. No ACTION_DOWN and no ACTION_UP.

I looked everywhere for a solution. I found one question on a forum that seems to be the same as my issue, but no solution is proposed: http://groups.google.com/group/android-developers/browse_thread/thread/9a9c23e40f02c134/bf12b89561f204ad?lnk=gst&q=ACTION_UP#bf12b89561f204ad

Here's my code:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.webkit.WebView;

public class Brand extends Activity {

public WebView webview;
public float currentXPosition;
public float currentYPosition;

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

    webview = new WebView(this);
    setContentView(webview);
    webview.loadUrl("file:///android_asset/Brand.html");


 }

@Override
public boolean onTouchEvent(MotionEvent me) {

    int action = me.getAction();
    currentXPosition = me.getX();
    currentYPosition = me.getY();

    Log.v("MotionEvent", "Action = " + action);
    Log.v("MotionEvent", "X = " + currentXPosition + "Y = " + currentYPosition);

    if (action == MotionEvent.ACTION_MOVE) {
         // do something
    }


    if (action == MotionEvent.ACTION_UP) {
         // do something
    }

    return true;
  }

}
like image 311
Dave Avatar asked Apr 09 '10 16:04

Dave


People also ask

How do you handle single and multi touch?

You can react to touch events in your custom views and your activities. Android supports multiple pointers, e.g. fingers which are interacting with the screen. The base class for touch support is the MotionEvent class which is passed to Views via the onTouchEvent() method. you override the onTouchEvent() method.

What is Action_up and Action_down?

ACTION_DOWN : When finger or object first comes in contact with the screen. The event contains the initial starting location of a gesture. ACTION_UP : When finger or object lifts from the screen. Contains the final release location of a gesture.

How do you use onInterceptTouchEvent?

If onInterceptTouchEvent() returns true , the MotionEvent is intercepted, meaning it is not passed on to the child, but rather to the onTouchEvent() method of the parent. The onInterceptTouchEvent() method gives a parent the chance to see any touch event before its children do.

Which motion event does a gesture start with?

A gesture starts with a motion event with ACTION_DOWN that provides the location of the first pointer down. As each additional pointer that goes down or up, the framework will generate a motion event with ACTION_POINTER_DOWN or ACTION_POINTER_UP accordingly.


1 Answers

First you need to follow the Webview tutorial to let a WebViewClient instead of the default browser do the work. Without that, after you load a web page, you would not get any events at all.

Assuming you want to handle the events in your acrtivity you can add the method dispatchTouchEvent:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    Log.v("FROM_DISPATCH_TOUCH_EVENT", "Action = " + ev.getAction());
    return false; 
}

You will see the other touch events as well. You should handle them in this method.

An alternative approuch is to register an EventListener. Your class would become:

public class Brand extends Activity implements OnTouchListener {

in onCreate() do this:

webview.setOnTouchListener(this);

an add this to the Activity:

@Override
public boolean onTouch(View v, MotionEvent event) {
   int action = event.getAction();
    float x = event.getX();
    float y = event.getY();

    Log.v("ON_TOUCH", "Action = " + action + " View:" + v.toString());
    Log.v("ON_TOUCH", "X = " + x + "Y = " + y);

return false;

}

From my logs I then get the following:

  1. dispatchTouchEvent() is called first
  2. Then onTouch() is called
  3. Then the View gets the Event to handle it
  4. Then onTouchEvent() might get events (see below)

Moreover, onTouch() reports relative to the View, dispatchTouchEvent() reports values relative to the Display.

Why does onTouchEvent not report these Events? Like many of these methods there are two incarnations of onTouchEvent().

If you override onTouchEvent() in your Activity it serves as a "catch all" - it only handles the events that have not been consumed by the Views, for example if you tap outside of any View, or the events these Views pass on. If you override onTouchEvent() in your View it it supposed to handle the events in that View. Sometimes it is easy to confuse the two.

like image 94
rgr_mt Avatar answered Oct 03 '22 14:10

rgr_mt