Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

The difference between View.getX() and View.getLeft()

I am wondering what the difference between View.getX() and View.getLeft() is. Thanks!

4 Answers

You're right: if the parent object has the full screen size, then getX and getLeft will return the same values. I would still strongly recommend to pick the one that matches the context of your scenario, though, don't just go with one randomly. You always want to pick the one that fits the functionality you are looking for. This will help keep your app functional, even if the screen size changes.

The reason they return floats is that you can position things in fractions of pixels. This is probably most important if you have apps that need to be precise and that deal in measurements, ratios, or similar. Also, if you have positions that are dynamically calculated, it will take the fraction into account, if there is one.

See here: http://developer.android.com/reference/android/view/View.html

getX is the horizontal position on the screen, while getLeft is the number of horizontal pixels in relation to its parent object. :)

Thanks Mike. Yea I saw the reference. I am wondering what if the parent of the object is the same as the screen. The getX and getLeft should return the same value right?

Also does getX also return the value of pixels? But why is it float?