Android view's getTop(), getLeft(), getX(), getY(), getWidth(), getHeight() methods
Asked Answered
O

3

17

I am writing a drag and drop application and got really confused because of some parameters.

Please help to figure out.

First of all, I read the documentation for the View class and got the following explanations.

getX() : The visual x position of this view, in pixels.

getY() : The visual y position of this view, in pixels.

getWidth() : Return the width of the your view.

getHeight() : Return the width of the your view.

getTop() : Top position of this view relative to its parent.

getLeft() : Left position of this view relative to its parent.

Now when we finished with the official documentation, let's see what do we have.

I have an image with original size 500x500 called circle.

enter image description here

And here's the actual screenshot of my application

enter image description here

Here is the xml for the layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/circle" />
</LinearLayout>

Now what am I concerned about. When I watch my locals, I get the following, which really confuses me.

enter image description here

I don't see any problem with the getX() and getY() functions, because they actually show me where does the image begin.

As the documentation states, the getWidth() and getHeight methods return the width and height of the view but the watch window tells me that my getWidth() and getHeight are 300, which I really can't understand, because in my XML I've set them 100dp each, so do the functions return me them in a different measurement, and how do I convert it to dp.

And finally, it tells me that getTop() and getLeft are 700 and 300, and as the documentation says, they are the position of the image relative to it's parent. But isn't my parent the Linear Layout, so what do this numbers mean in sense of screen positioning?

Octavia answered 12/5, 2015 at 22:16 Comment(0)
W
14

All these measurement methods return sizes in pixels( px ), not density-pixels ( dp ). If you want to convert it you can get the density by calling:

float density = getResources().getDisplayMetrics().density;

And then divide the values you get with the provided density, for example:

int widthDp = (int)(img.getWidth() / density);
Woozy answered 12/5, 2015 at 22:24 Comment(1)
@Carmine yes, see the documentation: developer.android.com/reference/android/view/View.html#getX()Woozy
T
19

This is a supplemental answer for future visitors.

enter image description here

  • Left, Top: When a parent view lays out a subview, left is the distance from the left side of the parent to the left side of the subview. Likewise, top is the distance from the top of the parent to the top of the subview. Thus, getLeft() and getTop() return the coordinates of the top left corner of the view relative to its parent view (not the absolute coordinates on the screen).
  • X, Y: Usually getX() and getY() will return the same thing as getLeft() and getTop(). However, sometimes it is useful to move the view a little after it has already been laid out. This can be done with setTranslationX() and setTranslationY(). If these have been set then x and y will be different from left and top, where

    x = left + translationX
    y = top + translationY
    
  • Width, Height: You can find the width and the height of the view with getWidth() and getHeight(). This is not affected by a translation.

The above values are all in pixel dimensions.

Tawannatawdry answered 15/10, 2018 at 5:53 Comment(0)
W
14

All these measurement methods return sizes in pixels( px ), not density-pixels ( dp ). If you want to convert it you can get the density by calling:

float density = getResources().getDisplayMetrics().density;

And then divide the values you get with the provided density, for example:

int widthDp = (int)(img.getWidth() / density);
Woozy answered 12/5, 2015 at 22:24 Comment(1)
@Carmine yes, see the documentation: developer.android.com/reference/android/view/View.html#getX()Woozy
L
6

You can get pixels from dp with

float ht_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, ht, getResources().getDisplayMetrics());
float wt_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, wt, getResources().getDisplayMetrics());

As of the positioning question. getTop and getLeft are relative values and are based on your parent. Since the only parent of your ImageView is LinearLayout you are effectively positioning your ImageView directly below the ActionBar/ToolBar

Also don't use an image for a circle, you can draw it easily with canvas.drawCircle it takes much less memory.

Lunular answered 12/5, 2015 at 22:27 Comment(3)
ok, but still one question remains for me, even when i divide by density, i get the left as 130, how come can it be 130 dp if it's right on the left side of the screen?Octavia
@Carmine getLeft() and getTop() give you positions of the view relative to its parent before its moved, in xml you set gravity to center, so its considered to be centered in parent.. getX() and getY() give you positions after you moveWoozy
the words before its moved explained everything))) thank you)Octavia

© 2022 - 2024 — McMap. All rights reserved.