28

I'm using the getX() and getY() method on a view for some special dragging logic (not animation, i.e. I never use setX/Y methods, I just need the getters to check).

However, I've come to realize that these are only available post-API 11.

The docs for getX() say that is it the addition of the 'left' property and the 'translationX' property. All well and good, except get/setTranslationX() is only around since API 11 as well.

I was wondering if there was any knowledge on what this method returns behind the scenes, so I could maybe put in a workaround.

Kara
  • 6,115
  • 16
  • 50
  • 57
cemulate
  • 2,305
  • 1
  • 34
  • 48

4 Answers4

16

Use nineOldAndroid.jar file in your project And use this way

import com.nineoldandroids.view.ViewHelper;


ViewHelper.setTranslationX(myView, translation);

ViewHelper.getX(myView);
Dipendra
  • 1,547
  • 19
  • 33
  • ViewCompat should be used as NineOldAndroids is deprecated, and ViewCompat is recommended. See [Roberto's answer](http://stackoverflow.com/a/37516278/3124150). – EmmanuelMess Jan 31 '17 at 13:36
14

How about getLeft() and getTop(). Looks to me like these are valid as long as the view hasn't been translated (setTranslationX() and setTranslationY()) which also aren't valid in the older API.

Doug Gerecht
  • 404
  • 4
  • 9
  • The method `getTop()` and `getLeft()`, give only the position relative to its parent, if you need the position in screen it doesn't work. – Ektos974 Mar 10 '14 at 09:45
  • 1
    Looks to me like getX() and getY() are also relative to the parent which, apparently, is what OP was looking for. If you want screen coordinates use getLocationOnScreen(). – Doug Gerecht Mar 10 '14 at 13:41
3

Using android.support.v4.view.ViewCompat the solution is:

ViewCompat.getY(mView);

which is compatible with old android devices.

Roberto Tellez Ibarra
  • 2,146
  • 3
  • 18
  • 34
0

i suppose it's not relevant anymore, but just in case somebody is looking for it: http://nineoldandroids.com/

marcin
  • 389
  • 2
  • 5
  • 14