views:

504

answers:

1

Hi

I made a custom view. If I add the view to the layout XML file and I set the height to "fill_parent" "specSize" return 0. Why?

Code:

    @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int measuredHeight = 90;

        int specMode = MeasureSpec.getMode(heightMeasureSpec);
        int specSize = MeasureSpec.getSize(MeasureSpec.getMode(heightMeasureSpec));

        if(specMode != MeasureSpec.UNSPECIFIED){
         measuredHeight = specSize;
        }
        setMeasuredDimension(60, measuredHeight);
}

Does anyone know how I can get the height of "fill_parent"?

Thanks!

+2  A: 

You don't need to call MeasureSpec.getMode inside of call to 'getSize' whole idea of measure spec is to combine way of measuring ( knowing as Spec) and associated Size. See documentation for MeasureSpec.makeMeasureSpec method.So correct code will look something like:

int widthSpec = MeasureSpec.getMode(widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
Nikolay Ivanov