Getting values from addOnPreDrawListener()

1.6k views Asked by At

I have not understood how to return values from the method below, I have tried to return int but it shows me a weird error which is not possible to solve. This is the code:

private void showTheEnemy() {

    ViewTreeObserver vto = radarImage.getViewTreeObserver();
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
            radarImage.getViewTreeObserver().removeOnPreDrawListener(this);
            yradar_height = radarImage.getMeasuredHeight();
            xradar_width = radarImage.getMeasuredWidth();
            return true;
        }
    });
    coordinateText.setText(yradar_height + " " + xradar_width);
}

I have understood that values are not being updated in the method because when I use setText() method then int values are 0.

2

There are 2 answers

2
OneCricketeer On BEST ANSWER

You get zeros because the inner-method is not triggered before you set the text. The values are set at whatever they were when you last initialized them.

Set the text from the callback.

private TextView coordinateText;
private int xradar_width = 0;
private int yradar_height = 0;

private void showTheEnemy() {

    ViewTreeObserver vto = radarImage.getViewTreeObserver();
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {

            yradar_height = radarImage.getMeasuredHeight();
            xradar_width = radarImage.getMeasuredWidth();

            coordinateText.setText(String.format("%d %d", 
                yradar_height,
                xradar_width 
            ));

            radarImage.getViewTreeObserver().removeOnPreDrawListener(this);
            return true;
        }
    });

    // Your values are still 0 here

}

Or pass to another method.

private TextView coordinateText;

private void showTheEnemy() {

    ViewTreeObserver vto = radarImage.getViewTreeObserver();
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {

         setCoordinate(coordinateText,  
                radarImage.getMeasuredHeight(),
                radarImage.getMeasuredWidth() ));
            radarImage.getViewTreeObserver().removeOnPreDrawListener(this);
            return true;
        }
    });

}

private static void setCoordinate(TextView tv, int xPos, int yPos) {
     tv.setText(String.format("%d %d", xPos, yPos));
}
0
bshears On

Set values when you get radarImage width and height:

private void showTheEnemy() {
    ViewTreeObserver vto = radarImage.getViewTreeObserver();
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
            radarImage.getViewTreeObserver().removeOnPreDrawListener(this);
            yradar_height = radarImage.getMeasuredHeight();
            xradar_width = radarImage.getMeasuredWidth();
            coordinateText.setText(yradar_height + " " + xradar_width);
            return true;
        }
    });
    coordinateText.setText(yradar_height + " " + xradar_width);
}

EDIT: Create class fields and when new values are provided pass those values where you need them.