I have a view on which I want to add an onClickListener. Trouble is that the listener fires even when the user performs a drag operation. How do I suppress on click when the user drags, and only process when it is a click without dragging?
For completeness, am using something like below to add the onClickListener -
private void installClickHandler(final GraphicalView x) {
x.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(getApplicationContext(), BillTablesActivity.class);
startActivity(myIntent);
}
}
});
}
set an onTouchListener(), which either returns true if you dragging, or returns false otherwise allowing your OnClickListener to handle the event. in other words, it's up to you to decide whether to treat the touch even as a click, or not.
to decide if something is a click or a drag (or a long press), the implementation of your OnTouchListener will have to keep track of ACTION_DOWN and ACTION_UP events, and compare the time between the two. a short interval would be a click, a longer time period would be a drag or a long press.
the specific implementation is left as an exercise for the reader :)
Related
My question is simple. But I am not getting. When panning then it also called the mChartView's OnClickListener. But I want when tap on graph then and then call OnClickListener
and when panning, disable the click.
I am giving my own question's answer. Just add the pan Listener to check the panning is continue or not. For that set a boolean and check condition in OnClickListener.
mChartView.addPanListener(new PanListener() {
#Override
public void panApplied() {
isPanEnables = true;
}
});
I have a view that can be swiped to another page. It has an onClick listener, which displays a dialog box.
The problem is, swiping triggers both actions (i.e. It shows the next page and the dialog box).
How can I disable the onClick listener when swiping.
It sounds like you want to use a GestureDetector (as well), the SimpleOnGestureListener has onSingleTapConfirmed() for click events and onFling() for swipe events.
While onClick of swipelayout put swipelayout.getSurfaceView() and while swiping it triggers only swipe action. See the code below:
holder.swipeLayout.getSurfaceView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do your onClick stuff here..
}
}
I'm using an opensource numberpicker I found somewhere (credit to Jeffrey F. Cole) but I just found a bug.
The numberpicker has a handler to increase the number faster when you touch the button
long`private Handler repeatUpdateHandler = new Handler();
`
class RepetetiveUpdater implements Runnable {
public void run() {
if (autoIncrement) {
increment();
repeatUpdateHandler.postDelayed(new RepetetiveUpdater(),
REPEAT_DELAY);
} else if (autoDecrement) {
decrement();
repeatUpdateHandler.postDelayed(new RepetetiveUpdater(),
REPEAT_DELAY);
}
}
}
.....
public class NumberPicker extends LinearLayout {
.....
// Auto increment for a long click
increment.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View arg0) {
autoIncrement = true;
repeatUpdateHandler.post(new RepetetiveUpdater());
return false;
}
});
// When the button is released, if we're auto incrementing, stop
increment.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP && autoIncrement) {
autoIncrement = false;
}
return false;
}
});
The problem is when you longclick the button the counter starts to increase, but when you hold your finger down and drag your finger across the screen the counter keeps adding up, even when you lift your finger.
So how can I detect that the finger gets out of my numberpicker layout and stop the counter?
Thx :)
I'm not so sure that's a bug and I'm not even sure it's coming from changes that this number picker made on top of the numberpicker from the API itself. I'm assumings that the buttons that handles incrementing and decrementing the pickers are set up to keep going until an ACTION_UP MotionEvent is received, but this might be over-simplifying it.
EDIT:
I've tested this on stock Android 2.3.3 and this is precisely the result.
EDIT:
Based on your clarification in the comments, this does sound like a pretty bad bug. Looks like what you need to do is have the Handler removes the callbacks to that runnable in ACTION_UP. Can you link me to the project so I can try to submit a patch?
EDIT
The NumberPicker you provided wasn't using Handlers correctly, IMO. Instead of keeping a reference to the same Handler so that callbacks could later be removed, it was created a new one everytime it posted. I've made some changes and fixed the issues here: https://gist.github.com/3657989
So I have a button that initiates communication to a server. However, when I try to disable the button from within the on click (to stop multiple clicks) it doesn't happen until the on click ends. This is a problem as the server communication means it takes a while for the method to execute. I saw a previous question that was answered by using flags to stop the multiple clicks (and it was stated that it is a known bug that buttons cannot be immediately inactivated). Is there now any other more elegant solution to this problem or is flags still the only way?
in Button's onClick()
You can,
button.setEnabled(false);
what is did when I had this problem is use a boolean flag for this, here is a sample code for using a flag:
boolean isClicked = false;
Button btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!isClicked) {
isClicked = true;
// CONNECT TO SERVER
isClicked = false;
}
}
You can hide the button,
button.setVisibility(View.INVISIBLE);
or Disable the button
button.setEnabled(false);
or Keep a flag using boolean
I want to write a program that handles touch events in a specific order within a single activity.
For example:
A few views are shown.
If the user doesn't tap on the first view, I will show another activity.
If the user taps on it however, I want to detect a tap on the second, then the third, etc.
How can I handle multiple touch events?
I think I need an onTouchEvent method and in it I need an if-else statement for the first click but I don't know how I can monitor for the subsequent touch events.
It may help you.I always do like this
public void onClick(View v){
if(v==imageView1){
//do ssomething
}
if(v== imageView2){
//do something
}
if(v==imageView3){
//do something
}
like this u can do according to different button or imageview
I'm assuming the picture is an ImageView inside the main view. Why not just append touchlisteners to each view?
Set the onClickListener for the n+1 view only when nth view is clicked.
Like this
view1.setOnClickListener(new OnClickListener() {
void onClick(View v) {
view2.setOnclickListner(new Onclicklistener() {
void onClick(View v) {
// add further view's click listeners else do what ever if this
// is the last view.
}
});
}
});
Not an elegant solution but should work IMHO.
From your problem statement it seems you have an ordered list of views, each should have a touch listener, but the listener for the second view should not fire unless the listener for the first view has fired first.
This can easily be done by keeping a counter in your activity:
private int highestIndexTapped. When a view is tapped, check whether its index is such that index == highestIndexTapped + 1.
If it is, increase highestIndexTapped by 1 and fire the listener. Otherwise either eat the touch event or pass it on to the next part of your pipeline.