Q:

Change the size of a Custom View at Runtime Android

I'm changing the height and the width of my CustomView which extends Android View in runtime like this:
        @Override
        public void onGlobalLayout() {          
            if(!initialized) {          
                int containerHeight = instance.getHeight();
                int containerWidth = instance.getWidth();
                myView.getLayoutParams().height = (int) (containerHeight * HEIGHT_RATIO);
                myView.getLayoutParams().width = (int) (containerWidth * WIDTH_RATIO);
                instance.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                initialized = true;
            }
        }

this code is in the container view Constructor.
In addition my CustomView onMeasure() is as follows:
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // maximum width we should use
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

This is the result:

Where the width and height I specified are at the same size of the green rectangle.
My Question is: why does the actual size of my custom view (red rectangle) is not at the same size as I gave as input in the LayoutParams ?

A:

My problem was in the drawing of the CustomView. In onDraw(), I took the width and height of the canvas instead of the View itself.

