onTouchListener with a new dialog - android

I have an app that inside onItemLongClick opens a dialog. I want the user to have a better experience so I want the possibility to choose and item in the dialog without having to raise his finger but I can't understand how to make a onTouch listener that will get the MOVE MotionEvent. can anyone tell me how to do this?
Thanks,
Benny.

Any onTouchListener will get move events, unless the moves are stolen from it. Given you're in an onItemLongClick, are you part of a ListView? If so, you'll get ACTION_CANCEL whenever the listview decides to take the touch events away from its children to scroll with, and you won't receive further moves (or even an up).

Related

How to intercept every touch action before the view's click listener handles it?

So, My original problem is that I have started working on a huge android application which has too many activities and layouts. It is totally undocumented/commented and I have to deliver results in days.
So, I want to match, the elements in layouts ->> to the code, so that I can understand the code faster.
If somehow I can define a global touch listener that can intercept every touch action and Log the associated view and it's parent layout, and after that hand the control over to the associated click listener.
Am I on the right path? is there any best practice in such scenarios?

Open Dialog with LongClick and focus the Dialog

I want to simulate the iOS 3D Touch. I have a recyclerview, where i register a OnLongClickListener to every item. When a longClick is detected, a AlertDialog opens.
For checking if the longClick ends, i have implemented the OnTouchlistener.
In short, i implemented it like it was described here: https://stackoverflow.com/a/10746549/4907047
It works like a charm, as long as i don't move the finger after longClick. If i move the finger, the listview under the dialog is still moving with my finger. Besides that the dialog will not close after stop touching the screen.
I think i have to cancel the events of the underlying listview. Does anyone know, how to handle this?
You should consider implementing a contextMenu since it can do what you want. Read this.

blinking in custom drag/tap implementation

I have implemented custom view with some children. The view can be scrolled using standard drag gesture. Also every child can be clicked. The problem is, that when I start dragging the view, one of children gets 'down' event and it changes its state to 'pressed' for a second. I would prefer standard listview behavior - the child goes into pressed state when the user keeps pressing this child with his/her finger for like 50ms. It would reduce blinking caused by misread press event.
I know, that I need at least 2 events to detect if the user is tapping or dragging the view. For now I'm using TimerTask to shedule 'down' event. When I get 'move' event before my 'down' event is executed, I know that the user is dragging and I can cancel the sheduled event.
I know it's quite hacky. I also tried gesturedetector to detect drag and tap events, but it needs some additional work to properly implement changing view state from pressed to default when the user moves finger and starts to drag the view.
My question is - how this is implemented in android listview? I tried to copy their solution from listview implementation, but it's so huge I can't handle it. Simply I don't see the code responsible for handling such situation.
I managed to understand a gesture detection logic in ListView and, in general, in android views. I wrote my own gesture detector, which is somewhat better than the original one. It reports more gestures (multiple taps, dragging) and has some configurables (timeouts, move epsilon). You can find it open-sourced here: Better Gesture Detector on code.google
The library uses Handler class and postDelayed()/removeCallbacks() method combination to detect, handle and cancel motion events and gestures. It's quite simple and one should be able to get the idea by just reading the code.
This repository also contains a simple demo. Please note that this code is provided 'as is', contains some useless comments, logs and should be cleaned up a bit.

process touch events across multiple scrollviews

I have two scrollviews side by side, I want the user to be able to drag list items back and forth from left to right scrollviews. However, I can't find a way to handle the touch events. I can't set a touch listener for each scrollview seperately as the drag gesture gets dropped when passing from one to another. I tried creating an absolute layout over the top of both, which works from the drag and drop perspective, but it stops me from being able to scroll the scrollviews. Is there a simple solution to this? can anyone help me out?
Generally, onTouchListener returns a boolean that indicates whether the touch has been handled. It's up to you to decide whether the touch was handled or not. When the user touches a View, Android will call it's touch listener. If the touch listener returns true, then it regards the touch as handled then moves on. If the touch listener returns false, then it will go up one to the parent view (in this case whatever your ScrollView is). Then the parent view's touch listener is called and must decide how to handle the touch. It will keep cascading up the parent views until a true is returned or until it reaches the end.
In your case, you may have to decide what the user has to do in order to drag & drop vs. scrolling. Perhaps the user must do a long press on an item before he/she can drag it or something.

Sliding finger across screen activating buttons as if they were pressed

In case my title wasn't clear enough, I'll explain it in detail:
Say we have a screen filled with multiple buttons (10+), and we press down on one, activating onTouch/onClick. If we now move the finger, without lifting it, I want it to activate any other button it slides over. In this particular case, I want sound to be played when you slide over a virtual piano.
I know about the onTouchListener-solution where you register every ACTION_MOVE and find some boundaries that activates new events, but that's far from optimal if you have multiple buttons and want to allow smooth sliding without delay.
I also read this thread which suggested that we combine the touchListener of the View with a gesturelistener from the activity, but once again, this does not feel at all optimal for my situation.
I have not tried combining touchlistener with gesturelistener yet, but I will go ahead and do so if someone tells me they have no other way of doing this.
In my opinion, the proper way of doing this is to forget about buttons, and create a custom view which draws the entire keyboard. In this view you can handle touch events the way you like. You do not even need the gesture detector, just analyze the action and coordinates of motion events, it's very easy.
I don't understand what you mean about ACTION_MOVE and delays. To minimize delay, react on ACTION_DOWN, and then on ACTION_MOVE if it hovers other keys while in down state. It can't be any faster than that. With buttons there is an important delay because the onClick() event is triggered when the user lift the finger, on ACTION_UP.
Buttons are just not meant to work as you describe. The idea is that if a user taps on a button and then move his finger away at the same time it does not trigger onClick events on other views around. This prevents bogus clicks.
I actually took the "easy" way out and used buttons with an onTouch-method using ACTION_DOWN and ACTION_MOVE with coordinate calculations that combined with event.getX() and event.getY() allows me to detect which button is currently hovered. It's lag free with 13 buttons.

Categories

Resources