I a view have for which I would like to show a ContextMenu on a longPress. I was able to get this ContextMenu to display using the recommended method of: calling activity.registerForContextmenu and overriding onCreateContextMenu(...).
However, I would like to do other things on other touch events, so my view has a TouchHandler assigned to it. When this touch handler is set, the onCreateContextMenu() never gets called (presumably because my TouchHandler is eating the longPress). So, is there anyway for me to instantiate and show a ContextMenu without the onCreateContenxtMenu() method being called?
Alternatively, I could just show my own custom dialog with my "menu" items. Is there any disadvantage to using a custom dialog instead of the ContextMenu?
One thing to try, is to return false from your OnTouchListener if you don't want the event to be consumed.
What do you return from OnTouchListener.OnTouch? Returning false means you haven't consumed the event, which should mean that other actions can be peformed on it as well.
Related
i need to implement a long press event on a checkbox in codenameone.
On normal buttons i use the longPointerPress method and a boolean to control if the short- or longpress event happens.
With the checkboxes i cannot find that option, it only toggles between checked/unchecked.
How is it possible to use a long press on a checkbox?
Thanks for your help!
I'm not familiar with codenameone, but in Android you can just use setOnLongClickListener since every View has that function.
longPress is a callback. You don't invoke it, you override it and it triggers when a longpress occurs. So Codename One invokes it internally. See the answer here: https://stackoverflow.com/a/54063388/756809
I need to create a click event, that's not being consumed, so that an underlying adapter receives the touch event.
I cant use an OnClickListener, because that would consume the event.
I tried my best with OnTouchListener, but if I return true the event gets consumed and if I return false the listener only registers ACTION_DOWN events.
How can I react on a click, but if its not a click the adapter will receive the move?
EDIT:
I'm using a lorentzos.swipecards SwipeFlingAdapterView, which uses touch-move-events. I put a layout over half of the view of the items of the adapter, which need to react on click events. But I either can't implement the click event, since I can't catch two events in a row without consuming them, or I can't have the SwipeFlingAdapterView react on the touch-move-event, since I, well, consumed it in the click event.
Don't know what sourcecode to share, since I tried many different ways and nothing works.
I know this got asked often, but all solutions(, I found,) don't work for me.
What I have is a CardView with an OnClickListener making a Toast (#toast1).
Inside the CardView there are multiple views as also a WebView.
As mentioned elsewhere, to pass through the click through the WebView to the CardView I have done following:
Set android:clickable="false" in WebView XML
insert following under CardView.setOnClickListener(...)
WebView.setOnTouchListener( (view, event) -> true);
I also replaced the lambda with an anonymous method, to see if its just this. No change.
What happens now is:
At the border and over the other views, the clickListener is triggered and the toast appears
Over the webView the clickListener isn't triggered.
Also put a toast (#toast2) in touchLstener of WebView before returning true, and it gets triggered.
What I expect:
Click will passed through WebView
With #toast2 added: First show #toast2 then #toast1
What is a bit confusing, that in documentation of OnTouchListener, the return is following:
True if the listener has consumed the event, false otherwise.
For me that means:
true: Don't pass click to below views, as listener consumed it
false: Pass click to below views, as listener didn't consumed it
But setting to false didn't change anything.
First of all I would suggest you to get familiar with android touch handling system - you can find a really good description in this answer. To sum it up: touch event propagation starts on top level of hierarchy, but actual handling of the touch event starts on the lowest level of view hierarchy. As for solution of your problem I may suggest to sublcass the parent of your WebView and override onInterceptTouchEvent in the following way:
#Override
public boolean onInterceptTouchEvent(MotionEvent e) {
return true;
}
This will instruct this parent view to intercept all touch events that would otherwise go to its children views, thus limiting the first level of touch processing to this view.
I've got a big headache and spent a few hours to solve my problem withou any reasonable results.
I use a custom adapter (extending ArrayAdapter) to show listview items. This adapter implements OnTouchListener. There's also a background selector which color is changing when an item is touched and OnItemClickListener in ListViewActivity.
What I need is capture touch events (all of them, not only ACTION_DOWN) in the adapter. When onTouch returns false, the consecutive events (ACTION_CANCEL, ACTION_UP etc.) aren't captured. On the other hand returning true stops dispatching all other events (e.g. click) so that the ListViewActivity's onItemClick is never triggered.
I tried hard to find any working solution in SO and other resources, but with no success.
One idea was not worry about click events and background. I may set the background programatically and trigger click event the same way when the onTouch action is ACTION_UP, but view.performClick() does nothing (view is the 1st argument of onTouch method).
[EDIT]
To make it clear, I want to handle touch events in the adapter beacuse I need the textview in the item to marquee when the user touches it. Thus, in onTouch, when the action equals ACTION_DOWN, I assign true to setSelected property of the textview and, consequently, false when ACTION_CANCEL or ACTION_UP.
Any suggestions?
Thanks in advance.
I have a simple list activity, for each list item I have onTouchEventListener class defined.
The problem is onFling event never occures, instead of it following sequence of events:
onDown, onShowPress, onLongPress. When I do the same but assign onTouchEventListener to the ListView instance then onFling happens as well but not when I assign it to the list items :(
Any thoughts?
Because of a possible bug, onDown method is needed to return true to make sure, that other events also occur. Thus, make sure your custom onDown always returns true.