1

I have created a custom view to pack 25 circles with different radii within the screen. For this, it involves many calculations that requires the getWidth(), getHeight(), getLeft(), getRight() etc. And I have exposed a method to set the data.

As soon as I set the data, I try to perform the calculation and then invalidate. But always, the getWidth(), getHeight(), getLeft(), getRight() returns zero

As a workaround, I performed the calculation in onLayout() override. So when I set data for the view, I invoke requestlayout() and invalidate(). This is working fine for the time being. But I feel this is not the right way to get it done.

Is there any recommended way of doing this.

1
  • use getMeasuredWidth() and getMeasuredHeight() Commented Jun 17, 2014 at 7:13

1 Answer 1

2

Actually what happens is, if we try to invoke the getHeight() and the getWidth() method from the onCreate() method. It is bound to return Null as we are trying to access the Image View's height or width even before the layout is loaded to the screen.

I could get it resolved using the following:

@Override
    public void onWindowFocusChanged (boolean hasFocus) {
        // the height will be set at this point
        Logger.show(Log.INFO, "", "profilePic.getHeight()....."+profilePic.getHeight());
        Logger.show(Log.INFO, "", "profilePic.getWidth()....."+profilePic.getWidth());
    }

onWindowFocusChanged() is called once the view is loaded and ready for user interaction.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.