I have been new to android. I have just gone through my slideshow app.
I want to make image slide using touch event just like facebook app. when we click on image and swipe it to left then new image shown and when swipe finger towards right then last old image is shown.
I don't know how this works and how to start with it. Can any one help me in this case to start with like providing tutorials and tips ?
I just want effect on the image.
Handling simple touch events works the following:
You need an OnTouchListener in your Activity. Add it by implementing the OnTouchListener interface.
You will have to override the onTouch(View v, MotionEvent event) method. This will be called if a touch gesture is recognized.
Now you get all the information about the Touch (in Android it is called MotionEvent) in the onTouch Method via the MotionEvent. Look at the MotionEvent-Class documentation to use the correct methods to handle your swipe gesture.
You could first ask what action really happened. Call getAction(). It will return the type of action. The easiest way would be to react to ACTION_MOVE.
Then ask for the origin of the touch event and the later position of the touch by using getHistoricalX(int)/getHistoricalY(int) and getX(int) and getY(int) (attention: these methods are only usable for ACTION_MOVE events - in your case it is ok).
Now as you have the start and end coords of the swipe gesture, you can calculate the length of the swipe in pixels and the direction of the swipe and that is all what you need.
Good Luck!
There's two bits of information that you might want. The first thing is, what you are referring to is called a gallery. There's several great examples of how to use a gallery, the basic idea is you get a BaseAdapter Class that gets the view and attach it to the gallery. See this tutorial for an example, or just google Android Gallery Example, you'll find alot of examples.
The second is gesture programming. Take a look at this tutorial for more information
Related
I am trying to implement drag and drop on a GridView in android (ICS), but when I drag the item to the edge of the screen, the GridView doesn't scroll. How can I implement this functionality?
First of all if you don't need to support android 2.x ( API <11 ) this Andorid Drag and Drop tutorial is what you are looking for. I've never tried that way cause i had to implement it on 2.x but i took a look at it seems pretty straight forward.
To implement the autoscroll i guess you could use ACTION_DRAG_EXITED or ACTION_DRAG_LOCATION on the grid view and fire the scroll manually when you see that the location is next to the view bounds.
If you instead have to implement it on 2.x then it's going to be much more hard, you basically need to implement all the DRAG events, or a subset of them, by yourself.
I did in once with a ListView and what you have to do is:
Override the gridView touchEvent.
Use the actions ACTION_MOVE, ACTION_DOWN, ACTION_UP to fire the Drag events
Enable the drawing cache on the GridView items( setDrawingCacheEnabled ) and use getDrawingCache() to copy the bitmap of the view on DRAG_START.
Than what you have to do is to use that bitmap drawn on top of the GridView at the position that the ACTION_MOVE event gives you.
it's not easy but if you take your time you can manage to get it.
what more, I haven't check today but maybe you can try to google a bit an see if someone took the time to implement it and release it Open Source.
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 have implemented custom view with some children. The view can be scrolled using standard drag gesture. Also every child can be clicked. The problem is, that when I start dragging the view, one of children gets 'down' event and it changes its state to 'pressed' for a second. I would prefer standard listview behavior - the child goes into pressed state when the user keeps pressing this child with his/her finger for like 50ms. It would reduce blinking caused by misread press event.
I know, that I need at least 2 events to detect if the user is tapping or dragging the view. For now I'm using TimerTask to shedule 'down' event. When I get 'move' event before my 'down' event is executed, I know that the user is dragging and I can cancel the sheduled event.
I know it's quite hacky. I also tried gesturedetector to detect drag and tap events, but it needs some additional work to properly implement changing view state from pressed to default when the user moves finger and starts to drag the view.
My question is - how this is implemented in android listview? I tried to copy their solution from listview implementation, but it's so huge I can't handle it. Simply I don't see the code responsible for handling such situation.
I managed to understand a gesture detection logic in ListView and, in general, in android views. I wrote my own gesture detector, which is somewhat better than the original one. It reports more gestures (multiple taps, dragging) and has some configurables (timeouts, move epsilon). You can find it open-sourced here: Better Gesture Detector on code.google
The library uses Handler class and postDelayed()/removeCallbacks() method combination to detect, handle and cancel motion events and gestures. It's quite simple and one should be able to get the idea by just reading the code.
This repository also contains a simple demo. Please note that this code is provided 'as is', contains some useless comments, logs and should be cleaned up a bit.
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
I have a scrollview in my layout and I want to be able to check if the user is scrolling. I checked the Scrollview in Android Docs, but I don't see anything like isScrolling() that returns a boolean.
My question is: is there a way to check if the user is attempting to scroll, as opposed to clicking a button? Could there be some other class in the Android core that is monitoring the scroll event?
There could be a better way but here is how you can figure it out.
You get a MotionEvent object inside the onTouchEvent(MotionEvent ev) method.
check the action of the motion event as ev.getAction().
If the action is ACTION_MOVE it implies that a scrolling is taking place.
Note: Scrolling is different than a fling. So the same logic cannot be used to determine if the fling is over.