How can I know when the button isn't clicked anymore - android

I have some problem - I must know when the button isn't clicked anymore. I need something like onClickListener which is executed when the user stops holding his finger on the button. Is it even possible?

Use onTouchListener and check for MotionEvent ACTION_UP (return true in your implementation to make Android know that you are handling the whole Touch event)

Yes. What you should do is register on touch handler:
button.setOnTouchListener(listener)
and in the listener you should check the event if it's action is ACTION_UP. That's all.

Related

How to make ontouchlistener work in such way it respond different when holding and when only touching?

I have a button , on which calling onTouchListener , I want to set different behavior . 1. if it is being pressed ,
2.when user just touch the button
You're probably better off using an OnLongClickListener rather than an onTouch. You can set one just like an onClick and it will call you when a press and hold has occurred.
If you did want to implement it via onTouch, you'd set a timer in the touchDown. If the user has a touchUp, cancel the timer. If not, when the timer goes off it is now a touch and hold and you perform whatever behavior you wanted to.

what is considered `click event` in android?

in iOS world, there's a concept of touchUpInside, touch started within a view and user lifted a finger within a view bound.
I see touch vs click (event listeners) in android, and I'd like to know the difference between them.
Touch events are much more basic. There's many varieties. The most common being:
DOWN: The user touch the item
MOVE: The user shifts around the item after touching it
UP: The user stops touch the item
CANCEL: The user shifts off the item but is still touching the screen.
In Android, a click event is a composite of touch events. In the case of click it's both DOWN and UP action. There are others that can be found in a GestureDetector. Fling for example is a combination of DOWN, MOVE, and UP in a fast motion that signifies the user swiped the finger really fast.
EDIT:
Clarification on how Android handles the true and false in onTouchEvent().
It's not very well documented, but the way the Views work in Android is that when you touch a View, it will send that touch event from the parent view to all children views. Each child will then send the event to it's children. This is done in the dispatchTouchEvent() method.
When a parent View receives false from the child's onTouchEvent() method, it will no longer send touch events to that child. Meaning that when you're view returns false in onTouchEvent(), your View is really says:
I am done processing touch events and all of my children are done as well.
90% of the time, in onTouchEvent() you would do return super.onTouchEvent() to return the touch values of all the children in the View.
So let's look at your click example. Here's one way to do it in which you return false on a DOWN event, but true on an UP.
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
return false;
case MotionEvent.ACTION_UP:
return true;
break;
default:
return false;
}
}
In this case, the parent of this View will receive false immediately. After which, it will stop sending touch events to this View because you said it was done.
Here's another way:
boolean mPossibleClick = false;
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPossibleClick = true;
break;
case MotionEvent.ACTION_UP:
if(mPossibleClick) {
// UP event immediately after DOWN. Perform click action
}
default:
mPossibleClick = false;
}
return mPossibleClick;
}
It's rather crude, but basically here's what will happen. When the user first touches it, you will receive DOWN which will return true. If the user lifts the finger, it will perform a click action after which will return false because the event is done. If the user moves his finger or any other action is performed, it will return false and the click will be nulled out.
That last one is not the best way to implement a click, but I think it illustrates what I'm trying to say. In real life, move events are extremely common if even for a couple pixels.
onClickListener is used whenever a click event for any view is raised, say for example: click event for Button, ImageButton.
onTouchListener is used whenever you want to implement Touch kind of functionality, say for example if you want to get co-ordinates of screen where you touch exactly.
Definitions:
onClickListner: Interface definition for a callback to be invoked when a view is clicked.
onTouchListener: Interface definition for a callback to be invoked when a touch event is dispatched to this view. The callback will be invoked before the touch event is given to the view.
Details:
onClickListener: http://developer.android.com/reference/android/view/View.OnClickListener.html
onTouchListener: http://developer.android.com/reference/android/view/View.OnTouchListener.html
refer to the response of PH7
which one is better to use?
It really depends on your requirement.
onTouch gives you Motion Event. Thus, you can do a lot of fancy things as it help you separate state of movement. Just to name a few
ACTION_UP
ACTION_DOWN
ACTION_MOVE
Those are common actions we usually implement to get desire result such as dragging view on screen.
On the other hand, onClick doesn't give you much except which view user interacts. onClick is a complete event comprising of focusing,pressing and releasing. So, you have little control over it. One side up is it is very simple to implement.
do we need to implement both?
It is not necessary unless you want to mess up with your user. If you just want simple click event, go for onClick. If you want more than click, go for onTouch. Doing both will complicate the process.
From User point of view, it is unnoticeable if you implement onTouch carefully to look like onClick.
for more details : refer this and this

How to distinguish Click event from touch event in onTouchEvent method when app is running in device?

I override the onTouchEvent method of Activty. In this method i would like to listen to actions: touch and click and give them two different reactions. The problem is when i test my app in emulator, everything works fine. But not in device. As in device, when i just click a button, i don't just get ACTION_DOWN and ACTION_UP but a few ACTION_MOVE signals after ACTION_DOWN.
Instead of overriding the TouchEvent on your Activity, assign a listener to the Button itself.
View view = getViewById(R.id.entire_view);
view.setOnTouchListener( ...

How to detect a motion event when an onClick listener is there?

I have a ListView with onItemClick() method . Now I want to detect a touch motion in the screen . Implementing onTouchEvent() does not work in this case since I have written already a click listener . So help me how to do it. I need both the on click listener and onTouchEvent().
You need to implement the OnTouchListener(View v, MotionEvent event) to check event.getX() and event.getY() when event.ACTION_DOWN and event.ACTION_UP and compare them to know the motion direction.
Note: you must return true to be able to receive the event.ACTION_UP event.

How to detect onLongClick ACTION_UP event?

I have an image which implements both OnClickListener and OnLongClickListener. In the OnLongClickListener at press down (ACTION_DOWN) I start a timer. I want to cancel it at ACTION_UP.
Now, if I implement OnTouchListener for this image, then I catch ACTION_DOWN for normal clicks. And I want to catch Long clicks ONLY.
Why does not long click fire ACTION_UP event? How can I detect this event and stop the timer.
If you need a code, I can post it, but I do not think it will help you as there is no errors in the code.
PS. I cannot stop the timer at any specific time or value as this long press continuously increase the value of another element. This increase process should stop only at image long press ACTION_UP.
Just use OnTouchListener and forget about OnLongClickListener. You can detect a long click by checking coordinates and pressed time, you have all that info in MotionEvent object passed by OnTouch().
1- To check coordinates, use getX() and getY() from MotionEvent and getDrawingRect() from your ImageView container.
2- To check for pressed time, use getEventTime(), storing first time in ACTION_DOWN and then compute by substracting with time in ACTION_UP event. You could assume like 800mS for a long click.

Categories

Resources