Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen on a View if the View change its XY position

How do I listen on a View to obtain its XY coordination if it is moving or its XY position change? The View is not necessary to be dragged (or by user touch event), it may change if the orientation change or pushed (upward) by the showing keyboard or etc. I want to know its updated XY coordination when it changes, how can I do that?

like image 444
Sam YC Avatar asked Dec 09 '13 01:12

Sam YC


1 Answers

You may do something like this

final View activityRootView = findViewById(R.id.mainLayout);
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
                new OnGlobalLayoutListener() {

                    @SuppressLint("NewApi")
                    @Override
                    public void onGlobalLayout() {
                        findViewById(R.id.textView).getX();
                        findViewById(R.id.textView).getY();

                    }
                });

Hope this helps.

like image 63
Chandrashekhar Avatar answered Sep 19 '22 11:09

Chandrashekhar