When application is started I run a custom pop-up till a user touches the screen. When screen is touched I catch it with event onTouch() and cancel the pop-up. From this point I don't need the event anymore.
The problem is the event is alive and continues to jump up every time a user touches the screen.
Is there any way to unsubscribe from this event? Something like in c# -= eventName.
The code is below:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (!_stopToast)
{
_hintToast.cancel();
_stopToast = true;
}
return false;
}
There's no such method (lets say removeTouchListener or similar) which will help you to remove an already defined touch listener from a view. Setting null to setOnTouchListener won't help too. What you can do is to create a new object reference of OnTouchListener class which does nothing and set it in setOnTouchListener. For example:
public final OnTouchListener dummyOnTouchListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent rawEvent) {
return false;
}
};
And simply use it as below:
yourView.setOnTouchListener(dummyOnTouchListener);
Related
My app has a View which is clickable, and then when this View is clicked, a handleClickEvent() should be called. I tried setting an OnClickListener to the view, but then when user is double tapping the view, the handleClickEvent() gets called twice. I don't want it to be called twice during double tap. I.e. I want handleClickEvent() be called ONCE for both single click and double tap.
I did some research and found out this question: Android detecting double tap without single tap first I tried what's suggested in the answer, i.e. implementing a GestureDetector with onSingleTapConfirmed() method. That worked, however I notice that there is a noticeable delay on the response time, comparing to using OnClickListener. I assume it is because onSingleTapConfirmed() is called only after the system confirms that this is a single tap by waiting for some certain time period.
Is there anyway to achieve this without having a noticeable delay? Another option I can think of is to have a lastClickedTimeStamp member variable, compare the current time when clicked with lastClickedTimeStamp, and if it is within some threshold don't do anything.
I am wondering if there are any other options?
Thank you!
The way I would do it is set a state variable as a long and call it timeOfLastTouch and set it to 0. Then in each OnClickListener function put this code in their
long currentTime = System.currentTimeMillis();
if (timeOfLastTouch + 100 < currentTime) {
// code
timeOfLastTouch = System.currentTimeMillis();
}
You can try using GestureDetector.SimpleOnGestureListener and combine it with onTouchEvent.
private GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d("SingleTap", " is detected");
return true;
}
#Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("DoubleTap", " is detected");
return true;
}
});
then, remove onClickListener and set touch event on that view
yourView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
You can refer to Detecting Common Gestures
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 an on click listener:
whiteKeyPressedArray[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}}
I see that this allows touches:
public boolean onTouch(View v, MotionEvent event) {
//Switch case for type of touch
}
But how can I detect touch rather than click on my whiteKeyPressedArray[i]?
Thanks!
OnTouch will fire many many times :), actually onTouch will be trigered over and over again as long as you keep your finger to that element (as long as you touch that element). Where onClick will be fire just ones but ONLY if you return false from your onTouch handler.
I don't know what the whiteKeyPressedArray[i] is, but have you tried:
whiteKeyPressedArray[i].setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return true; // or false if you want the event to pass on
}
});
Maybe this is what you are looking for?
I have an on touch listener for a webview, but it has a bad effect on the functionality of the webview, so I am wondering if there is anyway to removed the on touch listener after the initial interaction?
webView.setOnTouchListener(null);
So in you activity you would set your overridden onTouchListener:
mWebView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.setOnTouchListener(mWebView.mOnTouchListener);
return false;
}
});
And you would have to make a new class, extending WebView. And within it you would define an OnTouchListener.
public final OnTouchListener mOnTouchListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent rawEvent) {
return false;
}
};
Setting the ontouchlistener to null doesn't reset it to the default definition. You still have to provide an actual listener.
I was looking for help online and got to this post.
When I did
myView.setOnTouchListener(null);
my myView stopped responding to the onTouch.
I need to know when the user touches/taps/clicks the edittext in my activity.
How can I do this without interrupting the events, so the keypad still displays properly?
(And I need to know about it before the OS displays the keypad...if possible)
txtEdit.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View view, MotionEvent motionEvent) {
// your code here....
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
return false;
}
});
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
...
}
You should be able to do this by attaching an OnClickListener to your EditText. If you're concerned about blocking the UI thread in your OnClickListener, you can spawn a new Thread and do your work in there - though, if you do that, there's no guarantee the work will be done before the keypad shows up.
Less verbosity
The same Mathias Conradt's approach, but using kotlin:
txtEdit.setOnTouchListener({ view, motionEvent ->
// your code here....
false
})
This line getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); is unnecessary since the keyboard shown when you tap the editText view.
OnTouchListener was getting called twice for me (on Pixel 2 running Pie). So I used
OnFocusChangeListener instead:
txtEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
Log.d(TAG,"Focused Now!");
}
}
});