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;
}
}
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.
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.
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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With