Cote Mounyo
Cote Mounyo

Reputation: 13965

get position of view with respect to top-most parent in android

To get a view relative to its parent, I can use getLeft(), getRight(), getTop(), and getBottom(). But how do I get a view relative to the top-most parent of the layout file? So say my eve layout is RelativeLayout. Then say inside eve I have a number of nested layouts. And then deep within the hierarchy I have a EditText view. How do I get the location of the EditText view with respect to eve?

Upvotes: 7

Views: 10740

Answers (2)

isuekey
isuekey

Reputation: 59

View target = findViewById(R.id.target_id);
View parent = (View)target.getParent();
int x = target.getLeft();
int y = target.getTop();
while(parent != null){
   x += parent.getLeft();
   y += parent.getTop();
   parent = (View)parent.getParent()
}

These code may be help you, because I didn't test it. I hope it would work.

Upvotes: 4

Koso
Koso

Reputation: 3199

To find out absolute view location, you can use view.getLocationOnScreen() or view.getLocationInWindow().

Upvotes: 8

Related Questions