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?
To find out absolute view location, you can use view.getLocationOnScreen()
or view.getLocationInWindow()
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With