DragEvent.ACTION_DRAG_STARTED doesn't occurs - android

I'm working with Drag and Drop, everythings is OK, but, let consider this scene: i have a LinearLayout is parent view, and it have one child view is ImageView. LinearLayout is listen for OnDragListener, and ImageView is dragedView. So, when i drag the ImageView, the DragEvent.ACTION_DRAG_STARTED doesn't occurs, and then DragEvent.ACTION_DRAG_ENDED doesn't occurs too.
Mean while, the other actions are working as expected.
P/s: When working, there is one problem when i drag my ImageView (inside my LinearLayout) out side of my LinearLayout and drop it, so, my ImageView is disappear and there is no event is invoked. I want to detect this case to bring my ImageView turn back to it's original position.
Can anybody give me a suggestion?
Thanks!

accordding to the implementation of ViewGroup.dispatchDragEvent(), ViewGroup does not handle STARTED ENDED if any child has handled the event.
public boolean dispatchDragEvent(DragEvent event) {
.... // dispatch event to children
// If none of our children could handle the event, try here
if (!retval) {
// Call up to the View implementation that dispatches to installed listeners
retval = super.dispatchDragEvent(event);
}
}

Related

How to send event from custom linear layout to child view

I have a custom linearlayout in which I have two gridviews.
I want to have multitouch on the complete linearlayout, ie, first touch can be in one gridview and second touch can be in another gridview and the linear layout should be able to get multitouch events for this (ACTION_DOWN and ACTION_POINTER_DOWN). I am able to get this by overriding onInterceptTouchEvent in my custom layout.
Now I also want the gridview to scroll independently if required. Although gridview is a scrollable view, it is not able to do so, because I have intercepted the 'down' event. I see that when I try to scroll two events are generated : 'ACTION_DOWN' and 'ACTION_MOVE'.
Since I need ACTION_DOWN for multitouch use case I want it to be sent to linearlayout as well.
Is there a possibility to send the ACTION_DOWN event to both the linearlayout as well as my child view which is gridView in this case?
Need help.
You don't have to intercept ACTION_DOWN just to monitor that it flowed through your container view. In other words, you can override onInterceptTouchEvent() to monitor the touch events before they are passed to the appropriate child view by the framework, but as long as you don't screw with the return value it won't actually steal subsequent touch events from the grid elements. For example:
public boolean onInterceptTouchEvent (MotionEvent ev)
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
//Touchdown!
}
//Don't modify this and touch interaction will happen normally
return super.onInterceptTouchEvent(ev);
}
This allows you to see the event without taking it away from the child element(s).

Create a linearview on every activity to perfom action

I am trying to create view that sites underneath all others that will perform an action when 3 fingers are touched. These activities all have their own touches and swipes.
I have got this to kind of work by creating a class that extends linearlayout and intercepting touches. and adding this in xml.
But this is only being called when an item like button or view is a clickable or touchable surface and isn't becoming a touchable surface itself.
in
public class MyCatcher extends LinearLayout {
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
// multitouch!! - touch down
int count = event.getPointerCount(); // Number of 'fingers' in this
if (count > 3) {
doThis(mContext);
}
}
return super.onInterceptTouchEvent(event);
}
And in xml
<packagename.MyCatcher
....>
my normal layout
</packagename.MyCatcher>
So this code is working when an item that is touchable is touched but not becoming its own touch layer.
I have tried setontouchlister in the constructor with no luck.
Thanks for any help
EXTRA: When setting the touchlistener via onAttach I can get it to catch touches but I can't use event.getPointerCount() like normal as it's returning 1.
I'm worried this may take up lots of processing?/
Instead of a Layout, create a normal View and add it to your existing layout so that it spans the whole screen and lays "above" all other views (in z-order). Now it should get all touch events prior to all other views. Your onTouch... implementation should make sure to pass all touch events along to the other views (except if you want e.g. buttons not to work under certain circumstances, then do not pass the events along).
onintercepttouchevent()
Is called only whenever a touch event is occured. ie; touch event occurs only when a view item like button,textbox,etc having onclicklistener enabled is pressed . For your question you need to set the onclicklistener for the entire view.

Catch onTouch event by parent, handle it and pass to children

I tried to understand how Android handle touch event and got a little bit confused. From what I understand touch event are send to the root view and pass down to the children.
I have a FrameLayout that is a container for Fragment.
First fragment view is a ScrollView, second one is some kind of Gallery (HorizontalListView) and the last one is also FrameLayout. Only one fragment in the layout each time.
What I want to do is to identify user swipes on the screen, for the app use. I want to count the swipes and do something after some number of swipes.
I tried to put a OnTouchListener on the top FrameLayout but it doesn't get called when the child is the ScrollView or the Gallery. I tried to return false and also true in the end of onTouch, but I get same result - it's never being called.
How can I do it?
I just want to "transparently" handle the touch events and passing them on like I didn't even touch them.
My understanding is that it actually goes the other direction. The Child views get their event triggered first (sort of). The root view get's it's dispatchTouchEvent() called, which propagates the event down to the children's onTouchEvent(), and then, depending on whether they return true or false, the parent's onTouchEvent() is called.
The normal solution for intercepting things like this is to override dispatchTouchEvent(MotionEvent ev) in one's activity like so:
#Override
public boolean dispatchTouchEvent (MotionEvent ev) {
// Do your calcluations
return super.dispatchTouchEvent(ev);
}
The documentation for this one is here. Note that you can also override that method in any ViewGroup (such as a FrameLayout, etc)

Continuous touch event in Android

I have 4 buttons on an activity. I have set Touch Listeners for all the buttons.
Button1.setOnTouchListener(this);
Button2.setOnTouchListener(this);
Button3.setOnTouchListener(this);
Button4.setOnTouchListener(this);
All I want to do is to get MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP when I am moving my finger over the buttons.
I am getting these events when I am touching INDIVIDUAL buttons, but not when I move my finger from one button to another.
In this case, first button should get ACTION_UP message and the next button should get ACTION_DOWN message.
Kindly help.
There is a certain logic in such behavior of touchevents.
I think the best decision for you will be to set OnTouchListener to parent view of Button1, Button2, etc or override its OnTouchEvent() method. After that you will be able to send MotionEvent to child views (buttons in your case) manually when it's necessary.
My initial instinct is that you are sending all of the touch events to the same function, causing some type of bottleneck.
What if you seperate the touch handlers into four seperate functions like this - that way, all buttons have independent touch handlers and there is no collission by having them all using the same function to handle their collective events. Make a seperate "onTouchEventListener" for each button and have it look like this:
Button1.setOnTouchListener(button1Listener);
Button2.setOnTouchListener(button2Listener);
Button3.setOnTouchListener(button3Listener);
Button4.setOnTouchListener(button4Listener);
Here is how to do it from the Android docs:
// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
protected void onCreate(Bundle savedValues) {
...
// Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
// Register the onClick listener with the implementation above
button.setOnClickListener(mCorkyListener);
...
}
Since you're trying to capture a MotionEvent that involves all of the buttons, as opposed to just one, you will have to capture the MotionEvents in the parent view, then dispatch them to children buttons in the form of MotionEvent.ACTION_DOWN followed by MotionEvent.ACTION_UP.
I did something similar to make a multitouch keyboard out of a RelativeLayout containing many buttons. I basically overrided dispatchTouchEvent and kept track of each finger, sending ACITON_DOWN and ACTION_UP in place of ACTION_POINTER_DOWN and ACTION_POINTER_UP for secondary fingers to children.
It may be useful to look at the source of ViewGroup and see how it dispatches events to its children, and tweak the behavior to check when the finger goes out of child button bounds, and if so, send an ACTION_UP on the old button then an ACTION_DOWN on the new button.

Scrollview doesn't swipe when it's too short to scroll

I'm pretty new to Android app development, and I've been playing around with swipe gestures using Android's SimpleOnGestureListener and a ViewFlipper. There are 3 children of the ViewFlipper, and each is a ScrollView. They're all dynamically populated when the Activity loads, and they don't change after that. The ScrollView is where the SimpleOnGestureListeners are attached.
Here's the layout I'm using:
+ViewFlipper
++ScrollView (x3, one for each page, each with the following:)
+++LinearLayout (vertical)
++++TextView
++++TableLayout (dynamically populated w/TableRows)
++++View
I extended the onFling method with the common tutorial code you can find anywhere online, and it works great--except when one of the ScrollViews doesn't contain enough content to scroll.
I've narrowed the problem down to touch detection by overriding and calling super on every one of the SimpleOnGestureListener's methods to add a print-to-log.
When I swipe on a page that scrolls, I get something full of "in onClick" "in onScroll" "in onFling" etc. On a page that's too short to scroll, I get "in onClick" "in onShowPress" "in onLongPress", and that's only if I'm touching the content within the too-short scrollview's children--if I touch elsewhere I get no events at all.
Ideas on what's wrong, or how to detect the swipe gesture no matter how big the ScrollView is?
EDIT: I've determined that when I run this on an Android 2.2 emulator, as opposed to the Android 2.1u1 DroidX emulator I've been using, it goes away. This is reproducible across multiple environments.
I have some more insight on this; it seems as though onInterceptTouchEvent is not called for every motion event when a scrollview is contained within a flipper (or a WorkspaceView).
In particular, the behavior I found while modifying another view class to fix this very same issue (it is not unique to flippers) was as follows--note that this is Android 2.1 only:
If the scrollview is long enough to scroll, the ACTION_DOWN motion event is caught by the ScrollView, and every subsequent ACTION_MOVE event goes through onInterceptTouchEvent of the flipper, where it is intercepted and handled appropriately. In android 2.2, this behavior happens regardless of the scroll length.
Back to 2.1: If the scrollview is not long enough to scroll, the ACTION_DOWN motion event is not caught by the scrollview, but instead comes back to the onTouchEvent of the flipper. All subsequent ACTION_MOVE events of the same gesture skip the onInterceptTouchEvent function and go straight to the onTouchEvent function!
The way I resolved this was to take the functionality I had in onTouchEvent for ACTION_MOVE events and refactor it into its own method. In this way, I can have onTouchEvent call onInterceptTouchEvent followed by that functionality if it detects that the event has previously gone unhandled.
case MotionEvent.ACTION_MOVE:
if (touchState == TOUCH_STATE_SCROLLING) {
handleScrollMove(ev);
} else {
// Log.d("workspace","caught a move touch event but not scrolling");
//NOTE: We will never hit this case in Android 2.2. This is to fix a 2.1 bug.
//We need to do the work of interceptTouchEvent here because we don't intercept the move
//on children who don't scroll.
Log.d("workspace","handling move from onTouch");
if(onInterceptTouchEvent(ev) && touchState == TOUCH_STATE_SCROLLING){
handleScrollMove(ev);
}
}
break;
This is from WorkspaceView.java (a modification of Android's Workspace.java, found at the andro-views project on google code, and now here: Horizontal "tab"ish scroll between views ). In the case that we receive a move event, and we are scrolling (which only happens if we have deliberately chosen to intercept it--ie, it's set in the intercept function, so we've been to the intercept function already) we perform the move behavior we desire. If we receive a move event here and we are not scrolling, then we send the event back through onIntercept, and then see if we're now set to scrolling. If so, we perform the action.
It's not elegant, but it works!
I needed to create a new class that extended ScrollView, and used this:
#Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
return gestureDetector.onTouchEvent(event);
}
#Override
public boolean dispatchTouchEvent(MotionEvent ev){
gestureDetector.onTouchEvent(ev);
super.dispatchTouchEvent(ev);
return true;
}
I have no idea why, but if I try to return anything but true in dispatchTouchEvent (the logical thing would have been to
return (gestureDetector.onTouchEvent(ev) || super.dispatchTouchEvent(ev));
if I understand properly), it doesn't work, and this does.
Try setting android:fillViewport="true" in your layout xml for each of the ScrollViews. That tells the ScrollView to be as large as the view it's contained in.
Had the same issue. You need to intercept the touch event on the children of the ScrollView when it's too short to have a scrollbar.

Categories

Resources