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().
Related
So, My original problem is that I have started working on a huge android application which has too many activities and layouts. It is totally undocumented/commented and I have to deliver results in days.
So, I want to match, the elements in layouts ->> to the code, so that I can understand the code faster.
If somehow I can define a global touch listener that can intercept every touch action and Log the associated view and it's parent layout, and after that hand the control over to the associated click listener.
Am I on the right path? is there any best practice in such scenarios?
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).
The problem is the the scrolling will intercept with touch event has set to the parent layout.
Can I keep the onTouch event with the scroll in ScrollView ?
This is a very tricky part. There is an overriden method from Activity which is: public boolean onTouchEvent(MotionEvent event)
This is the general method that interprets all the touch events from the whole screen. And you could say, "ok, I can implement this and I am good to go..". And here comes the difficult part on how android works.
As you know every View has its own onTouchEvent() method that you could implement in order to add some custom implementation. So which method will listen? The ScrollView or the Activity? It appears that these touch events go from the "inside" elements to the "outside" elements. I mean parent-child relations.
Another thing to take into account is that the onTouchEvent method returns a boolean. This boolean parameter determines whether the touch event should go one level up or it is handled by the current View. Meaning that if you have a CustomViewA that implements the onTouchEvent() and CustomViewB implementing its own touch event, and the A is a child in B then the touch event would go through A first and if it is not handled it would go to B.
So basically yes it could be done. It depends on what touch event you wanted to do.
So in our case, the ScrollView returns true when the touch events are a horizontal. The activity's touch event will be handled only if the ScrollView touch event is not handled by itself then you are fine. Otherwise you have to override and implement the on touch event of scroll view and in some cases you have to return false so as for the whole layout to implement it. Good luck with the last part. I started to implement a fling effect but came up with some difficulties so I have implemented a 2 finger move with scroll view in it and it works like a charm.
This is about a week of research and experimenting and it is an overview of what I came up with. if you find anything else please let me know. Hope it helped.
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
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.