I use onTouch as it is the best and more modifiable than for an instance onClick. It is a custom view that is being touched that I want to respond with action, and I have allready limited so you can't just touch and it does it over and over. Now I need it to limit the amount of simountanious taps.
EDIT:
#Override
public boolean onTouch(View v, MotionEvent event) {
lines++;
return false;
}
I have prevented constant action, but not the possibility to press with more than 1 finger, so that if you tap with more the rest of the fingers get ignored
There are several ways to do it.
best for you may be limit it inside in your overrided onTouch event.
if(event.getPointerCount() > 1) {
System.out.println("Multitouch detected!");
return true;
}
else
return super.onTouchEvent(event);
Another option is set attribute android:splitMotionEvents = false in your xml file.
Related
I have created a GestureDetector to detect a double tap or long press. If either of them are triggered, I would like to disable any more touches. How can I do this?
Thanks!
Well there could be two ways to do this.
1. you have to implement onTouchListener along with a conditional variable say boolean. declare
Boolean isDouble = false; // your activity level variable
now when you have detected the double touch or longpress set it to true
isDouble = true;
now in your onTouchListener .
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(isDouble)
return false;
}
});
Other method may not work properly on some devices but it can work for your case.
view.setOnTouchListener(null);
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.
I have a TableRow with a custom background. When I click and hold my finger down, then drag out of the row and release without selecting anything (and thus generating a click event), the dark lines above and below the TableRow aren't being redrawn.
What even should I listen for, or, how might I refresh this Table so that the rows are redrawn if a touch is detected, but no selection is actually made?
Thanks everybody,
Tony
If you want to detect touch events (for a realease), can refer MotionEvent in Android, you have to override onTouchEvent(MotionEvent e)
You will get events for the Touch and then can match the action of the event (event.getAction) to ACTION_DOWN and ACTION_UP(for when touch is released)
Eg.
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_UP) {
//can check if selection is made or not and add function accordingly
return true;
} else {
return false;
}
}
How many columns do you have in your table, depending on your requirement, you can look up the ListView and custom adapter implementation with a notifySetDataChanged...check:
http://www.vogella.com/tutorials/AndroidListView/article.html
I am using onTouchListener for a layout. I want to take the click outside the layout. I set onTouchListetener for the layout. But motion event always shows ACTION_DOWN. Even i touchOutside the view, It is not showing ACTION_OUTSIDE. Could anyone help me to find out why it is not showing constant ACTION_OUTSIDE. Here is the code i am using
Layout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("action",event.getAction()+"");
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
Toast.makeText(getApplicationContext(), "check", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
If you return false, then you're signifying that you do not wish to receive further touch events. You need to return true to continue getting motion events.
Very simple to fix by adding this:
override fun onTouchEvent(event: MotionEvent): Boolean {
return true
}
The event listener itself listens to one event action at a time. The first of course is the ACTION.DOWN, in which your toast shows.
Just like what Jason Robinson and user936414, you have to return it to true so that the object or the listener could here the second event action, ACTION.OUTSIDE.
You will get ACTION_OUTSIDE when the touch is outside the activity and the flag FLAG_WATCH_OUTSIDE_TOUCH is set. Refer http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_WATCH_OUTSIDE_TOUCH
I have an App with 10 Buttons. Everytime the User presses on one Button A TextView should change. And if the User changes the focus and ,oves its finger to right, to the next button(without taking the finger off the screen) the seond button should be focused.
I tried it with setting an OnTouchListner to all buttons, but once the finger is moved the focus still stays on the first button:
btn1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
txt1.settext("1");
return false;
}
});
I hope you understood what I mean and can help me with this.
Thanks
Edit:
I found an Application which does that, here is a Video, so you can visualise what I mean.
Notice how I move the mouse(the finger in this case) and the Boxes change its focus:
http://www.youtube.com/watch?v=MRVFpNrBmsA&feature=youtu.be
To start in your onTouch() you should handle the events...
Something like
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
txt1.settext("1");
} else if(event.getAction() == MotionEvent.ACTION_HOVER_EXIT){
txt1.settext("0");
}
return true;
}
Not sure if those are the proper actions to check against but it is just to give the idea.
Also I believe you should return true otherwise it means that the event wasn't processed and it gets stuck.