New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why onTouchListener for button click? #27
Comments
Hello, I cannot seem to reproduce this error. I use an OnTouchListener to automatically dismiss BUTTON Type SuperActivityToasts/SuperCardToasts when the button is pressed since SuperActivityToasts/SuperCardToasts will lose reference to themselves upon orientation change recreation. In other words, if I did not do this calling superCardToast.dismiss() in an OnClickListener would work until the device is rotated. Since SuperActivityToasts/SuperCardToasts are reconstructed as new objects on orientation changes, any calls to the previous object would not do anything. I decided to use OnTouchListeners because they can return false which does not consume the touch event. This allows an OnTouchListener to call the onClick of an OnClickListener without any erratic behavior. The pivate OnTouchListener is as follows, it should call the onClick event before it dismisses. private OnTouchListener mButtonListener = new OnTouchListener() {
int timesTouched;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
/** Hack to prevent repeat touch events causing erratic behavior */
if (timesTouched == 0) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
if(mOnClickWrapper != null) {
mOnClickWrapper.onClick(view);
}
dismiss();
}
}
timesTouched++;
return false;
}
}; Do you have any code I can look at so I can look into this more? Thanks, John |
Hi John, thanks for the quick reply. I've been playing with the demo source for a few minutes and haven't reproduced the problem I'm having in my main project. I'll have another look at work tomorrow. I'm OK with the approach of using a Touch listener to dismiss the toast after button press, as you've described. But I think it should happen on ACTION_UP (after releasing the touch event.) The touch to dismiss should still happen on ACTION_DOWN. I don't have much time at the keyboard this week, but I'll play with it more when I do and report back. Thanks, |
Alex, Thank you for the suggestion! So what you're saying is have the onTouch event call the onClick event when the user presses the button and have it call dismiss() when he/she let's go? I'll have to give that a try. Keep me updated with the issue, even if you figure it out let me know what it was so I can see if the Wiki pages and examples are clear enough. Thanks!
|
Hi John, I think the behaviour should be consistent button presses elsewhere in the OS, and specifically like the undo toast in Google Now.
So basically the same thing that's happening now, just on ACTION_UP instead of ACTION_DOWN. The reason I specified the second point is because I quickly tried tweaking it to ACTION_UP and the onClick would be called even if I dragged my finger away from the button before releasing. There needs to be a check that the button still has focus when the ACTION_UP event fires. I've done this before and will let you know how I've done it when I can - I'm back in the office on Friday. Thanks for taking my suggestion! |
Alex, I think I'm going to change it to an OnClickListener after all. I looked at some other code (like Nurik's undobar) and he uses a private OnClickListener to call the on click event of the set listener. If he does it this way then I'm sure it's the right way to do it. I think I got confused about something else when I was talking about consuming the touch event. Anyway, I will change it to an OnClickListener but I will keep that OnTouchListener for the touch to dismiss function (setTouchToDismiss()). Thank you very much for your suggestion! If you have any more be sure to let me know.
|
This has been addressed in the develop branch, thanks! |
Using SuperCardToasts with buttons isn't working so well for me in 1.3.2.
The button seems to have an OnTouchListener that dismisses the toast before the wrapped OnClickListener is called.
Why are you using Touch listeners instead of Click listeners for the button?
The text was updated successfully, but these errors were encountered: