Android super.onTouch - android

I am adding a touch listener to a view.
here is the code:
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return super.onTouch(v, event);
}
});
But I keep getting the error:
The method onTouch(View, MotionEvent) is undefined for the type Object.
Why do I keep getting this error. Is there something I need to add to my app to make it work?

You must not call super in the onTouch-Method: Instead return false or true like you need it. (JavaDoc: True if the listener has consumed the event, false otherwise.)
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});

You're getting it because you're actually implementing an interface called View.OnTouchListener rather than extending the View

Related

After I used onTouchlistener then onclick listener stops working in recyclerview

I am using dragging feature for recycler view by using ItemTouch helper.
Dragging is working fine but I am unable to use onclick listener on the same view.
Below is my code:
holder.mainLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
mCommunicator.onStartDrag(holder);
return false;
}
});
holder.mainLayout.setOnClickListener(v -> {
//LogManager.v(LOG_TAG,"touch:"+blnOnTouch);
holder.checkBox.performClick();
});
Please suggest me for this issue. Thanks in advance.

Android: where is View.getOnTouchListener()?

I would like to implement a generic system that overrides all the onTouchListener
on a viewGroup and restore them later.
I tought about map to store each view's original listeners but I did not find any way to
get the currently assigned listener.
I need to do something like this:
View v;
View.onTouchListener backupListener = v.getOnTouchListener();
But unfortunately getOnTouchListener does not exists ? How can I do ?
if you use setOnTouchListener it's not overriding the touch Listener but just saving a pointer to another touch listener, so you can actually call to the original touch Listener:
view = findViewById(R.id.someView);
//alternative:
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("touch"," alternative");
return true;
}
});
//call to the original:
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return v.onTouchEvent(event);
}
});
you can check out the source code of setOnTouchListener();

Activity.onTouchEvent isn't called after returning from background

On my activity I override the onTouchEvent method:
#Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "MyActivity.onTouchEvent");
...
return false;
}
That works as expected until the app goes to the background, when it returns that method isn't being called anymore.
To try and understand what's going on I added a touch listener to the root view:
findViewById(android.R.id.content).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
Log.d((TAG, "content.onTouch view: " + view);
return false;
}
}
I get the same output for both scenarios, before and after going to background, in both cases the view it prints is the same: android:id/content.
Since that's not "my view", I also tried:
((ViewGroup) findViewById(android.R.id.content)).getChildAt(0).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
Log.d((TAG, "content.onTouch view: " + view);
return false;
}
}
Which acts the same in both cases (just the view it prints is the main layout in my xml file).
Any idea why the OnTouchListener is called even after returning from background but the onTouchEvent isn't?
There aren't any other views that I could find that return true for the touch event, and the printing of the view in the listener also proves that.
I've been trying to figure it out for the last two days now but to no avail. Would love to get any kind of a clue or direction.
Thanks.

Android turning clicks into touches

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?

Is there anyway to remove an onTouchListener from a view object?

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.

Categories

Resources