2

A View has a getLeft() method, but how do I setLeft?

I want to do this so that I can move a RelativeLayout as the user drags their finger sideways across the screen. It is the same size and width as the screen, so as it moves it will need to move off the edge of the screen.

I have experimented with the various animation classes, but I don't think this is what I want in this scenario.

I'm targeting phones rather than tablets, so this needs to work for API level 7.

Richard Ev
  • 52,939
  • 59
  • 191
  • 278

2 Answers2

4

I usually do things like this with View.offsetTopAndBottom() and/or View.offsetLeftAndRight() and adjust the view position with each touch/scroll update. Also make sure that the View you are trying to move around isn't inside a layout that will be restricting you from doing so.

HTH.

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • Thanks, this works but the `RelativeLayout` that is being moved also resizes, so that it still fits within the bounds of the screen. Is it possible to move it so that it does not resize, and instead moves off the edge of the screen? – Richard Ev Nov 01 '11 at 21:09
  • This may be a product of the layout parameters you have set on the view your moving with respect to it's parent. If something is set to `fill_parent` or `match_parent` it will try to attach itself of the outer bound of the parent view, even while it moves. – devunwired Nov 01 '11 at 21:11
  • I thought that might be the case, so changed the width to `wrap_content`. The `RelativeLayout` still gets resized to fit in the confines of the screen. – Richard Ev Nov 01 '11 at 21:12
  • I use (indirectly, through `ViewDragHelper`) these methods to drag a View inside a `LinearLayout`. The problem is that every time this LinearLayout `onLayout()` method is called, the position of the view is reset (see my question http://stackoverflow.com/questions/22865291/why-is-view-dragged-with-viewdraghelper-reset-to-its-original-position-on-layout). Is there any way to avoid this? (I don't have this type of problem when I drag a View by modifying its margins) – Sébastien Apr 05 '14 at 09:48
0

I'm not too sure if this is what you were looking for, but there is no setLeft() method or anything else that directly sets the coordinates. What I would try is getting the LayoutParams and setting the left manually; like so: view.getLayoutParams().left = 123 I'm not confident that this will work because it may depend on what type of ViewGroup the view is in (ie. LinearLayout). Good luck!

Brian
  • 7,955
  • 16
  • 66
  • 107
  • Oh you might also want to try `view.requestLayout()` to refresh it after you set the coordinates. – Brian Nov 01 '11 at 20:39
  • `getLayoutParams()` returns a `ViewGroup.LayoutParams` object, which sadly does not have a `left` property – Richard Ev Nov 01 '11 at 20:53
  • 1
    What type of `ViewGroup` is your view in? LinearLayout? If it is try type casting it `(LinearLayout.LayoutParams)getLayoutParams()` – Brian Nov 01 '11 at 21:31