3

I began to study Android not so long ago and have a question relating to which approach I should use to solve the simple task. Let's suppose I have a view (maybe, a button) and I want user to be able to move it across the screen with a finger. Until AbsoluteLayout was deprecated, the right approach seamed obvious. I would've just changed position of my view based on corresponding events. But what is right now?

Andrey Kon
  • 1,099
  • 2
  • 9
  • 18

3 Answers3

5

Create a custom view of your own and add a onTouch event listener. It is very simple. Explained very well here.

Sudarshan Bhat
  • 3,772
  • 2
  • 26
  • 53
0

If you're trying to move your views in order to navigate through your application or page through images in a gallery, Android provides a collection of widgets to do things like that. If you're trying to, say, pan across a large image, maybe this will help: Image in Canvas with touch events

Community
  • 1
  • 1
Sparky
  • 8,437
  • 1
  • 29
  • 41
0

Im using FrameLayout and update marginLeft and marginTop

You can add multiple children to frame layout, though this is not recommended because of multiple resolutions issues.

from the android docs:

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.

This "warning" is not applicable in your case because you are explicitly setting the margins according to the user touch event.

ravyoli
  • 698
  • 6
  • 13