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
Related
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).
Hi all in my android app I move objects on screen as per user input.
Basically I have to take care of two inputs - drag and UP(touch & remove)
So I implemented Gesture listener where the two methods -
onScroll() & onSingleTapUp() were used to handle these two input events.
But onSingleTapUp() does not work if the user has kept her finger for a long time on screen and then lifted it up.
So the conditions now require me implement onTouch to harness the MotionEvent.ACTION_UP functionality.
But would it be a good idea to use both onTouch & Gesture Listener.
Or should I shift my code to onTouch and remove gesture listener ?
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().
I have a custom view with an ImageView and a TextView on it and implemented the onClickListener for my custom view.
The problem is, that the ImageView is consuming the onClick-event (I just want the user be able to click on my control, no matter where). I could listen to the onClick of the Image/TextView too, but it seems dirty to me.
Is there a way to bubble / route Events in Android? Or possible another good solution?
View.onClick() event does not bubble. Two possible solutions:
Register OnCLickListener on you child views and then pass on the event by calling performClick() on parent.
Use OnTouchListener which bubbles up: just return false in child view's onTouch() method. This is more work as you have to account for touch-down & lift-up in order to emulate click.
Have you set the onClickListener in your custom view?
Set your custom view as clickable.
I don't recommend on setting any click listener in the child views.
Does it work now?
Actually I'm trying to implement an ontouchlistener into my android (1.5) application.
therefore i implemented the "ontouchlistener" into the class, and then i put my code into the:
public boolean onTouchEvent (MotionEvent e){
//code
}
The problem is, that if I am dragging over an (e.g.) Spinner or EditTextView than this Method isn't called. The only way to solve this which i figured out is to add an ontouchlistener to each of this views manually:
((Spinner)(findViewById(R.id.spinner1))).setOnTouchListener(tl);
((Spinner)(findViewById(R.id.spinner2))).setOnTouchListener(tl);
(tl is the ontouchlistener)
So isn't there a way to catch the touch event before it gets to each of those views?
thanks in advance
Ripei
What exactly do you want to do - do you want to:
Detect whole gestures in some
Activity.
Detect touches only for a given
View
If you want the former:
Take a look at this example:
Fling gesture detection on grid layout.
Basically, you redirect all registered touch events to a GestureDetector.
It understands what exactly is the user input (single tap, double tap, scroll, etc.) and calls the corresponding callback in a SimpleOnGestureListener, which you should implement.
If you want the latter:
You can either:
Override the View's onTouchEvent().
Implement and hook onTouchListener (if you want to get the touch before it's dispatched to it).
If you want to implement onTouchListener, you'll be doing your work not in the onTouchEvent() method, but in the interface's onTouch() method.
Hope that helps.