onClick listener drag consideration - android

Why is the android onclick listener for any views which we listen using setOnClickListener() is not working as expected, I mean onclick similar to desktop mouse event, onclick should be fired when mouse DOWN and UP events are triggered at the same location, but in case of android, after ACTION_DOWN if I move a little and release touch, I still get onclick fired. This is causing some unwanted effect on my TextView, which has links.
Is it because there will be always slightest difference in up and down coordinates? the reason for poor implementation.
USE CASE
Suppose in onclick I need to toggle ActionBar visibility but I do not want that to happen when I swipe which again fire onclick. For I was hoping for a clean code using android inbuilt options instead of using flags around touch listeners.

The onClick method is called on the release (ACTION_UP).
If you want to track the DOWN and UP actions yourself you can use an onTouchListener instead (see http://developer.android.com/reference/android/view/View.OnTouchListener.html).

Related

android why swipe on a textview will trigger onclick event?

I am using the default project template from Android Studio. When I swipe on the textView, the onClick event will get triggered. Is it the designed behavior or where did I do something wrong?
In my experience OnClick is triggered because when you put your finger on textView its ACTION_DOWN method gets called internally and after swipe when you remove your finger its ACTION_UP method gets called internally. So whenever these two combination gets called it calls the onClick method. So when you touch (ACTION_DOWN) the textView and swipe left/right and move your finger outside (without the ACTION_UP event) it won't get called because its ACTION_UP is not called.
PS. This is not the official definition/working of onClick... This is how it worked in my experience.
my solution is extending the parent viewgroup and examining motionevent in the onInterceptTouchEvent.

View.setClickable(false) but ACTION_DOWN events fired

I am trying to understand what clickable in Android means. I have a simple Button subclass which overrides onTouchEvent() and also implements OnTouchListener.onTouch() (returns false to let the event bubble up to the view) and I noticed that the callbacks are called with MotionEvent.ACTION_DOWN (listener first, the view method later), and nothing more. I thought clickable = false disables touch events completely, but apparently not. What does it really do? How can I make sure the view doesn't get any touch events at all (setEnabled(false) seems not to work at all, events for ACTION_DOWN, _MOVE and _UP are fired unhindered).
Why are some touch events handled and other are not for clickable = false? I find it pretty surprising behavior and inconsistent - I would expect MotionEvent.ACTION_UP to be fired as well, but maybe click listeners should not be called. Any guidance?
Update With setEnabled(false) only the View.onTouchEvent method is called, the listener is ignored. Even stranger...
As I understand, setClickable is for calling the onClickListener when you click on it, and the actions like ACTION_DOWN, ACTION_MOVE, ACTION_UP are for handling finger events on the View within a OnTouchListener.

Android OnClick and onTouch : Which one is better?

I am developing a android application, in which we have standard login registration pages.
I like to ask about onClick listener and onTouch listener, which action listener is better for implementing actions on buttons. And there are any effects if I used any of listener?
Use onClick for regular button clicks.
onTouch is used for gesture detection (swiping, tapping, double-tapping, etc.)
I think onTouch will call when you touch if u not removed your finger also.
and onClick will call once you click and remove finger

Handling onClick and onTouchEvent events concurrently

I have a button that I handle through an onClick handler.
However, I also need to respond to onTouchEvent events so I can handle button hilighting.
The problem is, if I return "false" from my onTouchEvent handler, it will call in the onClick handler, but will never give me an onTouchEvent for the subsequent "up" event.
If I return "true" from onTouchEvent, I ill get the "up" event, but it will never dispatch the touch to onClick.
So what are my options?
Implement my own "click" handling inside onTouchEvent - which means that I would be required to track moves in and outside the button area, etc. Seems kind of complicated.
I could have my button use a selector instead of a single image, and assign different images for different states. The problem I have with this is that selectors are cumbersome to change the images, which I need to do. (When my app is in different "modes", the button images for different states change. Again, this seems overly complicated.
Isn't there a simple way to accomplish all this???
When you need both click and touch events on the same view, use a GestureDetector. You should spefically look into the onShowPress method of its listener.
It seems like your best bet will stem from option 2, above.
I would suggest creating multiple Button StateListDrawable selectors, and then programmatically switching between styles using Button.setBackgroundResource().

How can I drag a imagebutton in android

I'm using ImageButton's in my application. By using onTouch event and onTouchListener I'm responding to the touch events.
Once the touch event was completed I'm just displaying the ImageButton at target location.
Instead I wanted it to look like dragging of ImageButton. Can anyone point me in the right direction.
This blog has two very nice examples of how you can use touch events to move any kind of view around the screen.
I'm not sure I understand what the problem is. You wrote "Once the touch event was completed [...]" - does that mean you've got it to work in one situation but not others? Are you considering that the touch event is completed when the action of the MotionEvent is MotionEvent.ACTION_UP? If you've got that part working, then why not just re-use that same code for the action MotionEvent.ACTION_MOVE?
Define a own view wich is extending imagebutton, define a drawable for "active" state and set (or reset) the drawable onclick

Categories

Resources