implementing ontouchListener on emulator - android

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.

Related

kotlin surfaceview ontouchlistener

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.

Listener when hovering view Android

The user can draw over view (with a pencil) and i want a listener to know if the user was drawn over other views. When the user is drawing over a view, i want to change the view's color.
I tried with OnTouchListener, OnHoverListener, OnFocusChangeListener i also tried to override the method onTouchEvent, but none of these were called.
Screenshot of the sample:
http://imgur.com/u3tkwRN
You should handle onTouchListener by calling ViewObject.setOnTouchListener the OnTouchListener passes to argument . The first is the Calling View and the second is a MotionEvent object . The motion event has a getAction() method than returns a integer you should write a if statement and check whether the getAction() value equals to MotionEvent.ACTION_DOWN . In the block of the if statement write your code . Also you can handle unhover event by write another if statement and check whether the getAction() integer equals to MotionEvent.ACTION_UP and write another code in if block statement . Your problem will be solved completely.

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.

Onkeydown what's the diference between return false and return super.onkeydown()?

I put a list view inside a linearlayout,and I want to override onkeydown() method in ListView
,and I don't control the focus, just change some variables in onkeydown(),I want system do as if I haven't override the onkeydown method.what should I do?return false? or return super.onkeyDown()?.It will be very nice for any help. thank you in advance.
there are three listviews,and a b c are three item of listviews. when c is focused, when I press left arrow on keybord, then a get focus by default. And I want the first item in listview2 get focus how can I do?
ntc is not exactly correct. OS does not use reflection to get your base class' handler.
If you return false, you explicitly tell OS that you do not want to handle this event; OS calls the View's parent handler then (as your test shows); this happens until event got handled or top View is reached.
If you call super.OnKeyDown() you allow your base class to process event. Note that it's a base class' handler serving same view, not parent view; don't confuse here.
So, using one or the other depends on your view's behavior desired.
I supose in your case you need return super.onKeyDown();
super.onkeyDown()- this puts the burden on super class to handle the onKeyDown event by your own wish (You explicitly say to handle it). when you return false, android assumes that you have not handled the onKeyDown event and super.onKeyDown() gets called by default (without you calling it).

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.

Categories

Resources