Android turning clicks into touches - android

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?

Related

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();

Android Event Propagation

The Android input event documentation here seems to imply that returning false in an event listener will cause the event to continue on to other listeners. For example
OnTouchListener touchListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// should continue on to other views
return false;
}
};
The problem is if I have two buttons in the same layout and I click on one I would expect the event to continue on to all views since I'm returning false, for example:
Button submitButton = (Button) findViewById(R.id.submit_button);
submitButton.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("--> submitButton");
return false;
}
});
Button clearButton = (Button) findViewById(R.id.clear_button);
clearButton.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("--> clearButton");
return false;
}
});
Since the submit button's listener returns false I would expect the click result to be sent on to the clear button but the output is always:
--> submitButton
--> submitButton
Can someone explain how events are propagated within a view since I seem to be misunderstanding the article?
The events are propagated from the parent views to the child views. Never to sibling views.
What you are trying would work if your clearButton were inside the submitButton (witch is not even possible).
You could mimic that behaviour with performClick()

How to unsubscribe from onTouch() event

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);

Android - How to detect a single tap on a LinearLayout?

I have a View and I need to detect a single tap on a LinearLayout.
I don't care about movements, All I want to detect is a single tap.
Basically a touch detection just like the touch detection on buttons. How can I achieve this?
myView.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
// This gets called so many times on every movement
return true;
}
});
I'm pretty sure, this would work aryaxt:
myView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// myView tapped //
}
});

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