droid-test
droid-test

Reputation: 31

Android: Problem with onMeasure()

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?

Upvotes: 3

Views: 6722

Answers (2)

Nitin Rajurkar
Nitin Rajurkar

Reputation: 11

The correct code will be:

int specMode = MeasureSpec.getMode(heightMeasureSpec);     ----This was already correct
int specSize = MeasureSpec.getSize(heightMeasureSpec);     ----This is corrected.

Upvotes: 0

Nikolay Ivanov
Nikolay Ivanov

Reputation: 8935

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);

Upvotes: 3

Related Questions