android touch event - android

I have two views, view A and B.
When I trigger the touch event "move" on view A, View B will show and overlay on the View A. At the same time, I want View B can receive the same touch event "move" from View A. Is it possible to send the same touch event to View B from system?

Yes, you can call the onTouchEvent() method of viewB and pass it the MotionEvent that was given to viewA Like this:
viewA.setOnTouchListener(new OnTouchListener() {
#Override
public onTouch(View v, MotionEvent me){
viewB.onTouchEvent(me);
}
});
That should pass all of the events that come to viewA on to viewB.

Related

give multitouch to view A, and A is behind to B View

I have two view A and B
A is MATCH_PARENET (width, height) and B is also MATCH_PARENT (width, height)
A view is covered by B View and B view is INVISIBLE
I have given Multitouch listener to A, but because of A is covered by B, I can not touch to A view because on top of A there is INVISIBLE, B view
Can any one help me how to do zoom-in out, rotate and move to view A
I have done by one way but it is not properly working
multiTouchListener = new MultiTouchListener();
a.setOnTouchListener(multiTouchListener);
b.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
multiTouchListener.onTouch(a, event);
return true;
}
});
I have given b's touch event to a,
but right now a is constantly moving if I simple drag my finger and stop
prevent B from getting touches, add these lines to the xml properties of B:
<View
...
android:focusable="false",
android:focusableInTouchMode="false",
android:clickable="false" />
make sure you do not add any click/touch listeners in code to B, as it'll revert those properties.

"Nephew" views block pinch gesture

I have a hierachy of custom views build as following:
Custom View A has a few View B's as children
Custom View B is like a button and needs to react to click/tap
Custom View C has a pinch gesture on it and is a sibling of A, and is under A in the view hierarchy
When pinching only on the Custom View A, everything goes well, as A does not catch any motion event and the view under it, C, receives the events and makes the pinch gesture (zoom in/out) correctly.
When tapping the Custom View B, the view is clicked and it is all good.
The issue is, when trying to pinch and one finger starts on View B, it catches its events and those are not propagated under it and no pinching ever happens on the Custom View C.
I would like to have View B react to tap only and pass all other events (pinch) to the view underneath itself, in this case Custom View C.
How can I achieve that scenario?
Found the solution!
We try to find a TapConfirmed event (using a gestureDetector to find it), and we dispatch the event directly to the sibling to pass the event:
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (conditionToIntercept) {
return true;
}
return false;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if(conditionToIntercept{
if (!gestureDetectorCompat.onTouchEvent(event)) {
siblingView.dispatchTouchEvent(event);
return true;
}
}
return false;
}

Android: Custom View in ScrollView

I created custom View similar to SeekBar. Now when I touch regular SeekBar and slide it work even I move finger out of SeekBar. But when I touch my custom View and move finger out of it ScrollView start moving and my View do not recieve MotionEven any more.
How can I make it acting same way as SeekBar?
You need to disable touch event of the parent view when the touch event is cached by you view, like this:
#Override
public boolean onTouch(View v, MotionEvent event) {
// Disallow the touch request for parent scroll on touch of child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}

Android - ignore parent view touch on child view touch

I have a layout like
RelativeLayout_Parent
-> RelativeLayout_Child
both views having touch event.
But when I touch on RelativeLayout_Child, touch event for parent is also fired.
How to ignore parent view touch on child view touch?
Very simple, implement OnTouchListener over your child view, and upon receiving Touch Event just return true fro child, this will make sure touch event is not propagated to others.
child_view.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// this will make sure event is not propagated to others, nesting same view area
return true;
}
});

How to detect only a particular gesture in SimpleOnGestureListener?

I want to change activity only on a particular gesture on a particular view.
For example
on Activity A -
double tap on View 1 should change current Activity to B
swipe on View 2 should change current Activity to C
In the OnTouchListener, you can pass the view that triggered the event:
public boolean onTouch(View v, final MotionEvent event){... this.onClick(v); ...}
just call the functions (IE: onClick) and then check if the view is the one you need to trigger this event:
public void onClick(View v){
switch (v.getId()){
case R.id.X: {DO THIS}
case R.id.Y: {DO THAT}
Implement the following Listners
1) GestureDetector.OnDoubleTapListener
2) OnGesturePerformedListener
One for Double Tab and Second listner for Swipe, as you will get unimplemented methods call your activity from there.

Categories

Resources