Vihaan Verma
Vihaan Verma

Reputation: 13143

what is the difference between getTop and getY

I don't seem to understand the difference much between getTop() and getY() in android Views. How are they different ?

Upvotes: 9

Views: 8699

Answers (4)

King of Masses
King of Masses

Reputation: 18775

First of all, read the documentation for the View class:

As per my knowledge:

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 height of the your view.

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

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

Upvotes: 0

Emre Aktürk
Emre Aktürk

Reputation: 3346

getY() method return the Y coordinate according to parent.

On other hand getTop() returns the Y coordinate according to its parent view. If parent has 300 as Y point, and the other view inside of it just a bit lower than it, then returns 100 unlike getY method returns 300+100.

Upvotes: 3

S.Thiongane
S.Thiongane

Reputation: 6905

The doc :

getTop () : Top position of this view relative to its parent. Returns The top of this view, in pixels.

And :

getY () :The visual y position of this view, in pixels. This is equivalent to the translationY property plus the current top property. Returns The visual y position of this view, in pixels.

Upvotes: 1

laalto
laalto

Reputation: 152887

getTop() returns the y coordinate relative to the parent.

getY() returns the y coordinate relative to the parent like getTop(), plus the y translation as returned by getTranslationY().

For questions like this it's often helpful to consult the source:

public final int getTop() {
    return mTop;
}

http://androidxref.com/5.1.0_r1/xref/frameworks/base/core/java/android/view/View.java#10644

public float getY() {
    return mTop + getTranslationY();
}

http://androidxref.com/5.1.0_r1/xref/frameworks/base/core/java/android/view/View.java#10908

Upvotes: 11

Related Questions