Dave
Dave

Reputation: 265

Android beginner: understanding MotionEvent actions

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

}

Upvotes: 13

Views: 45151

Answers (2)

rgr_mt
rgr_mt

Reputation: 985

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.

Upvotes: 12

Steve Haley
Steve Haley

Reputation: 55714

Your problem is that you're doing this from within a WebView. WebViews have built-in touch control to allow the user to scroll the page around, and that must be interfering with you receiving the MotionEvents. It would seem that you will need to use a different View. For example, I just ran your code making the following substitution:

TextView tv = new TextView(this);
setContentView(tv);

instead of

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

and everything else worked. Note: I used a TextView as the whole view just for simplicity. This works on ViewGroups like LinearLayouts, RelativeLayouts, custom Views with canvases etc. It just seems that WebViews are special. (I was also only receiving ACTION_MOVE when using the WebView.) So, I hope you can get away with using something other than a WebView!

Upvotes: 10

Related Questions