Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 881
Cote Mounyo Avatar asked Jul 12 '13 20:07

Cote Mounyo


2 Answers

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

like image 119
Koso Avatar answered Sep 20 '22 12:09

Koso


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.

like image 27
isuekey Avatar answered Sep 18 '22 12:09

isuekey