Android: Switch touch event focus view - android

I am catching the touch events on an ImageView, when I touch it the view gets hidden and all the view is covered by a FrameLayout. I need that the touch events on the image view to stop and to transfer a new touch event on the new layout, which is covering this ImageView.
I have tried something like this:
imageView.setOnTouchEventListener { v, event ->
imageView.clearFocus()
frameLayout.requestFocus()
return false
}
I have tried things as dispatchEvent but it does not work in this case since the imageView is just a little part of the whole screen and the frameLayout covers all the screen.
Anyways I always end up with the same problem, where I have to lift my finger and touch the screen again so the imageView looses the old touch event and finally the framelayout starts catching it. What can I do to solve this?

you can implement touch event listener on both ImageView and FrameLayout then use a variable to know ImageView is touched or not. something like this:
boolean imageViewIsTouched = false;
imageView.setOnTouchEventListener { v, event ->
if(!imageViewIsTouched){
//do something
imageViewIsTouched = true;
}
return false
}
frameLayout.setOnTouchEventListener { v, event ->
if(imageViewIsTouched){
//do something
}
return false
}

Related

Android: Intercept touch gesture handling from the parent to the child view

So I have the following view structure:
LinearLayout
HorizontalScrollView
Other Child Views
The parent LinearLayout is clickable has a custom selector (changes color when pressed). I want to be able to touch the HorizontalScrollView within the LinearLayout and still handle the touch in the LinearLayout as long as it is not a scroll motion. If I do a scroll motion then the HorizontalScrollView should intercept the gesture and cancel the touch for the LinearLayout. Basically, I want to be able to intercept the gesture from a child view as opposed from the parent which is the standard.
I have tried to handle the MotionEvent manually by creating extension classes that do the following:
LinearLayout
public override bool OnInterceptTouchEvent(MotionEvent ev)
{
// Handle the motion event even if a child returned true for OnTouchEvent
base.OnTouchEvent(ev);
return base.OnInterceptTouchEvent(ev);
}
HorizontalScrollView
public override bool OnTouchEvent(MotionEvent e)
{
if (e.Action == MotionEventActions.Down)
{
_intialXPos = e.GetX();
}
if (e.Action == MotionEventActions.Move)
{
float xDifference = Math.Abs(e.GetX() - _intialXPos);
if (xDifference > _touchSlop)
{
// Prevent the parent OnInterceptTouchEvent from being called, thus it will no longer be able to handle motion events for this gesture
Parent.RequestDisallowInterceptTouchEvent(true);
}
}
return base.OnTouchEvent(e);
}
This almost worked. When I touch the HorizontalScrollView, the LinearLayout shows the pressed state UI and activates when the click is completed. If I touch and scroll the HorizontalScrollView then scrolling works. When I let go of the scroll, the click handler for the LinearLayout does not fire because it was intercepted. But the problem is that before I start scrolling the LinearLayout changes to the pressed state and it does not reset even after the gesture is completed. In my additional attempt to try to manually cancel the gesture for the LinearLayout I kept running into other issues. Additionally, the LinearyLayout has other buttons inside it which when clicked should not allow the parent LinearLayout to display the pressed state. Any suggestions? Is there a set pattern for intercepting touch events from a child? I'm sure it is possible if both classes know about each other, but I am trying to avoid coupling them.
The following work for me for all cases:
InterceptableLinearLayout
public override bool DispatchTouchEvent(MotionEvent e)
{
bool dispatched = base.DispatchTouchEvent(e);
// Handle the motion event even if a child returns true in OnTouchEvent
// The MotionEvent may have been canceled by the child view
base.OnTouchEvent(e);
return dispatched;
}
public override bool OnTouchEvent(MotionEvent e)
{
// We are calling OnTouchEvent manually, if OnTouchEvent propagates back to this layout do nothing as it was already handled.
return true;
}
InterceptCapableChildView
public override bool OnTouchEvent(MotionEvent e)
{
bool handledTouch = base.OnTouchEvent(e);
if ([Meets Condition to Intercept Gesture])
{
// If we are inside an interceptable viewgroup, intercept the motionevent by sending the cancel action to the parent
e.Action = MotionEventActions.Cancel;
}
return handledTouch;
}

Android - Pressed / Touched item in a ListView

Is it possible to know in a ListView if a item is pressed / touched (but not clicked) and know which?
I tried with "OnTouchListener" but without success (I can intercept UP and Down events on the ListView but not on the elements).
I tried also "OnItemLongClickListener" but I have to wait when I want information immediately.
Do you have an idea ?
Thank you in advance.
Edit (solution) :
I put the following code in my adapter in the item view.
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
image.setImageResource(R.drawable.image2);
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
image.setImageResource(R.drawable.image1);
}
return true;
}
});
And now, when I touch an item, the picture becomes "image2" and when I do not touch the screen, it returns to "image1".
But there is a problem, if I press an item (the image2 appears well) and I move my finger in the list view and I do not touch the screen, it stays on for the image2, "MotionEvent.ACTION_UP" could not execute.
Do you have any idea how to do that as soon as I do not touch the screen, it must return on image1?
Thank you for your help.
You can set SetOnTouchListener to item view when your adapter create it.
you just try yo implement the Listener then you can override the function or please add your rough work part

Pass ontouchlistener to parent

I have 4 buttons in my activity and each of these buttons have a onTouchListener. I want to pass that event to the button's parent which is a Linear Layout. To achieve that, I have used
THIS :
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
public boolean onInterceptTouchEvent(MotionEvent ev)
{
onTouchEvent(ev);
return true;
}
});
but it doesn't work. What could be the problem?
OnTouchListener documentation indicates that onTouch() returns
True if the listener has consumed the event, false otherwise.
So since you return true, you indicate that the touch listener of your button consumed the event, and the even doesn't get propagated any further.
Returning false instead will make the event be propagated further up the view hierarchy.
Note though, that a button which unconditionally doesn't listen to touch events isn't a button. I would make sure whether a TextView for example isn't enough.
Although this would be true and sufficient for a simple TextView (or any View for that matter), you should note that Button is a clickable view and by default behaves as such. This means that whatever you return as a result of onTouch(), the touch event won't be propagated further and the Button's onClick() method will be called.
To disable this behavior and have it behave as you expect, just make it non-clickable:
button.setClickable(false);
Again, using a button doesn't make much sense anymore though.

SeekBar inside a draggable view (SlideHolder)

I use a 3rd party component (AndroidSideMenu) which allows the whole activity to be dragged to the right to expose a side menu.
In the main (non-menu) section of the layout I have a horizontal SeekBar.
The problem is that when I start dragging the SeekBar button, the whole layout drags along with it because the SlideHolder component is also responding to the drag events.
I'm not sure how touch events are managed in Android, but is there a way to stop the events after they are processed by the SeekBar so that they don't reach the parent container?
So when you drag the Seekbar, the whole screen (parent) must stay put, but when you drag outside the SeekBar it must still work as required for the side menu?
Thanks
In the container of the SeekBar, create and apply (to the seekBar) an OnTouchListener like the following:
private final OnTouchListener onTouchListener = new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
// If the View in question isn't a child of the seekBar and
// isn't the seekBar itself, then return and don't consume the event.
if ( (seekBar.findViewById(v.getId()) == null) &&
(v.getId() != seekBar.getId()) )
{
return false;
}
// The View is either a child of the seekBar or is the seekBar itself,
// so we pass the event along to the seekBar and return true, signifying
// that the event has been consumed.
seekBar.onTouchEvent(event);
return true;
}
}
Try this out and see if it works; you may need to create a more robust condition for the if statement.

OnTouchEvent not working on child views

I have a Linear Layout that has a Button and a TextView on it. I have written a OnTouchEvent for the activity. The code works fine if I touch on the screen, but if I touch the button the code does not work. What is the possible solution for this?
public boolean onTouchEvent(MotionEvent event) {
int eventaction=event.getAction();
switch(eventaction)
{
case MotionEvent.ACTION_MOVE:
reg.setText("hey");
break;
}
return true;
}
The problem is the order of operations for how Android handles touch events. Each touch event follows the pattern of (simplified example):
Activity.dispatchTouchEvent()
ViewGroup.dispatchTouchEvent()
View.dispatchTouchEvent()
View.onTouchEvent()
ViewGroup.onTouchEvent()
Activity.onTouchEvent()
But events only follow the chain until they are consumed (meaning somebody returns true from onTouchEvent() or a listener). In the case where you just touch somewhere on the screen, nobody is interested in the event, so it flows all the way down to your code. However, in the case of a button (or other clickable View) it consumes the touch event because it is interested in it, so the flow stops at Line 4.
If you want to monitor all touches that go into your Activity, you need to override dispatchTouchEvent() since that what always gets called first, onTouchEvent() for an Activity gets called last, and only if nobody else captured the event. Be careful to not consume events here, though, or the child views will never get them and your buttons won't be clickable.
public boolean dispatchTouchEvent(MotionEvent event) {
int eventaction=event.getAction();
switch(eventaction) {
case MotionEvent.ACTION_MOVE:
reg.setText("hey");
break;
default:
break;
}
return super.dispatchTouchEvent(event);
}
Another option would be to put your touch handling code into a custom ViewGroup (like LinearLayout) and use its onInterceptTouchEvent() method to allow the parent view to steal away and handle touch events when necessary. Be careful though, as this interaction is one that cannot be undone until a new touch event begins (once you steal one event, you steal them all).
HTH
Let me add one more comment to this excellent post by #Devunwired.
If you've also set an onTouchListener on your View, then its onTouch() method will be called AFTER the dispatch methods, but BEFORE any onTouchEvent() method, i.e. in between no.3 and no.4 on #Devunwired's answer.
Try to set the descendantFocusability attribute of your layout to blocksDescendants
Activity::onTouchEvent will be called only when non of the views in the Activity WIndow consumes/handles the event. If you touch the Button, the Button will consume the events, so the Activity won't be able to handle it.
Check out following articles for more about Android Touch Event handling pipeline.
http://pierrchen.blogspot.jp/2014/03/pipeline-of-android-touch-event-handling.html
you can also try onUserInteraction():
#Override
public void onUserInteraction(){
//your code here
super.onUserInteraction();
}
works well for me!
RecyclerView list_view = findViewById(R.id.list_view);
list_view.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener(){
#Override
public boolean onInterceptTouchEvent(#NonNull RecyclerView rv, #NonNull MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
Log.i("Hello", "World");
return false;
}
});
use public boolean dispatchTouchEvent(MotionEvent event) instead on onTouchEvent()

Categories

Resources