kotlin surfaceview ontouchlistener - android

I am working on android surface view with touch events.
Below is the code I wrote for surface view to handle touch events.
As shown in the image, I logged when the listener is called.
Then in when clause I handled ACTION_DOWN and ACTION_MOVE events.
However, when I touch the view, not only the when clause can't catch the event
but OnTouchListener is even not called too.
I also don't understand why trues in when clause are not used.
I wrote those expressions to tell OnTouchListener to return true.
Can anyone help me?
+) I figured out that my surfaceview doens't call onClick() too.
particleSurfaceView.setOnClickListener{ Log.d("sv", "click") }
never logs

Try making the touch listener always return true. If you ever return false, you stop getting notifications for that whole gesture / touch.

Well, it was Android Studio problem..
Touch event works but only the logging didn't work.
I have no idea why but after relaunching the Android Studio, it works.

Related

SimpleOnGestureListener: onDoubleTap disabled when onSingleTapConfirmed is implemented

I am implementing a GestureDetector.SimpleOnGestureListener.
I first implemented the onDoubleTap() method and had it working fine. I then implemented the onSingleTapConfirmed() method. I find that when this method exists in my listener, the onDoubleTap method is greyed out indicating it is never called. Sure enough, if I run the app, the double tap no longer is called. This happens even if the onSingleTapConfirmed() just returns false.
I don't see any logical reason why the listener and respond to both single and double taps. Anyone know why this would be?
I can't comment, but show your code, for me both methods works.
Also you can try to implement onDoubleTapEvent() method (not only onDoubleTap(), if it makes any change for you.
Maybe also setting listener gestureDetector.setOnDoubleTapListener(listener) would help, but I'm not using it.

Android dispatchTouchEvent, onTouchEvent, onTouch stop working after 1-5 minutes

I develop OpenGL ES 2.0 (GLSurfaceView) android game. Touch events stop working after sometime 1-5 minutes. I do not add any subviews. I have ad networks integrated but internet is turned off.
First I implemented onTouch method from OnTouchListener interface. Then I implemented onTouchEvent in GLSurfaceView and later dispatchTouchEvent
All these method get called for the first few minutes but stop get called. It is not related to game play.
What may course these issues?
I wrapped code in onTouch with try/catch block and put breakpoint in catch to ensure there is no exception in that method.
If you lock the UI thread then it will block touch but will not block GL thread. It will also block runOnUiThread calls.
Most likely there is something intercepting, and consuming the touch event that is "above" your surface view. I wonder if your ad network is perhaps displaying some sort of invisible ad? (invisible because there is no network?). Do you see the problem when you remove the ad network?

implementing ontouchListener on emulator

I want to implement ontouchlistener but i don't have actual device to test it.I have already implemented onclicklistener.how can I implement both onclickLisrener and ontouhLisrener with emulator.
thank you
Return values of onClick method and onTouch method set to false.
If you set "return true;", the Android stop to find event listeners.
So you want to enable two or more event listeners, you have to set return value false.

How to get onTouchEvent, long click and context menu working together?

In our application we have a custom view (that extends ImageView) and in it we handle the touch events to record data. I wanted to add context menu functionality to this view and followed the guidelines in the official Android documents.
The onTouchEvent code works fine by itself. The context menu code also works fine. However, if I add them both, the context menu code stops working. I found out that with both pieces of code added, onCreateContextMenu is never called, therefore context menu is never displayed.
According to my interpretation of the Android documentation, returning false from onTouchEvent indicates that the event is not consumed, so it should be used for further processing. For some reason, it is not happening here. I would appreciate if anybody can tell me what I am missing. BTW, the target is Nexus One running 2.3.4 ROM.
Here's the code for the onTouchEvent in the custom view:
public boolean onTouchEvent(MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
// Add event coordinates to an arraylist
break;
}
return false;
}
Thank you in advance for any help.
Elaborating on hackbod answer, you should probably have as last method statement return super.onTouchEvent(event);.
I guess that if you don't process the event, and if you don't invoke the default View behavior, than no one will do anything, and nothing will happen.
The point of return value might be for example to invoke some ancestor' default behavior, and let the derived class know if the ancestor processed the event or not.
After doing some search on Android Developers, referring to the topic override an existing callback method for the View here it says :
This allows you to define the default behavior for each event inside your custom View and determine whether the event should be passed on to some other child View.
Hence the main idea behind the return value is to let Android know whether the event should be passed down to child Views or not.
HTH
Edit:
Regarding the "directions" you mention in your comment, generally speaking (i.e. not only on Android) the UI event handling process goes on something like this:
At some point your derived custom control receives the event. In your event hook implementation, it's up to you whether to involve your ancestor's behavior or not. That's all you got regarding the class inheritance direction.
Then, there's the other direction, the one related to the UI controls hierarchy. Your custom control might be contained in one larger control container, and your control might as well contain other inner controls (textboxes, buttons, ...). Regarding this direction, if you declare not to process the event (returning false) then the UI event handling mechanism will pass the bucket to the containing (?) control (think the one on the background of yours).
You could call, from your long click listener,
openContextMenu(View view)
http://developer.android.com/reference/android/app/Activity.html#openContextMenu(android.view.View)
Do not register for context menu in OnCreate(), do it in onTouch() before
return true;
registerForContextMenu(View v);
openContextMenu(View v);
return true;
Returning false tells the parent that you didn't consume the event. The default implementation of View implements touch handling for that view; if you want that to execute, you must call super.onTouchEvent(event);
I encounter similar problem recently. When I enable long clickable in RecyeclerView's child, the ACTION_DOWN event can't not be received in RecyclerView's onTouchEvent.
If I changed to RecyclerView's dispatchTouchEvent, I would works. The ACTION_DOWN event can be received.

Does onInterceptTouchEvent() really works as the sdk said?

ths sdk said:
3、For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().
4、If you return true from here, you will not receive any following events: the target view will receive the same event but with the action ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here.
But when i use this method,no matter what onInterceptTouchEvent() returns ,it does the same work! And never did MotionEvent.ACTION_MOVE or MotionEvent.ACTION_UP been captured by this method. Can anybody help me figure it out?
ths!
Normally the touch goes from most upper view to the lowest through onInterceptTouchEvent and then it goes back via onTouchEvent.
If you return true in onInterceptTouchEvent you forbid it to continue and the view where you returned true is the last one to receive the touch, you consume it
You can also disallow your parent view to consume the event by
requestDisallowInterceptTouchEvent(true);
When none of the children of your view return true in onTouchEvent, onInterceptTouchEvent will ONLY be called for MotionEvent.ACTION_DOWN.
Here is a complete description of the MotionEvent processing.

Categories

Resources