Question

I am using OnGlobalLayoutListener. How can I use data from this listener? Especially I need lAngle.

        past_edittext.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {
                    int height = past_edittext.getHeight();
                    int width = past_edittext.getWidth();
                    int top = past_edittext.getTop();
                    int left = past_edittext.getLeft();

                    // center coordinates of EditText
                    past_edittextX = left + width / 2;
                    past_edittextY = top + height / 2;
                    lAngle = (float) (Math
                            .atan((totalCenterY - past_edittextY)
                                    / (totalCenterX - past_edittextX)) * 180 / Math.PI);
                }
            });
Was it helpful?

Solution

in your class declare the following

private float lAngle;

then you can access lAngle after you've set it from the globallayoutlistener

...
past_edittext.getViewTreeObserver().addOnGlobalLayoutListener(
        new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                int height = past_edittext.getHeight();
                int width = past_edittext.getWidth();
                int top = past_edittext.getTop();
                int left = past_edittext.getLeft();

                // center coordinates of EditText
                past_edittextX = left + width / 2;
                past_edittextY = top + height / 2;
                lAngle = (float) (Math
                        .atan((totalCenterY - past_edittextY)
                                / (totalCenterX - past_edittextX)) * 180 / Math.PI);
            }
        });
}

public void someOtherMethod(){
    if (lAngle != null)
        // do something...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top