On MeasureSpec

satya - Tue Oct 23 2012 08:46:25 GMT-0400 (Eastern Daylight Time)

MeasureSpec unspecified at_most exact

MeasureSpec unspecified at_most exact

Search for: MeasureSpec unspecified at_most exact

satya - Tue Oct 23 2012 08:52:47 GMT-0400 (Eastern Daylight Time)

Brief summary on what I know so far

wrap_content will result in AT_MOST wit the remaining entire size in the layout. match_parent will result in EXACT with the parents size.

Because wrap_content will result in AT_MOST you should override onmeasure and return the optimal size that your view wants. The default doesn't do that. The default behavior for onmeasure is to take the 'entire space' suggested by the at_most spec.

The unspecified spec seem to be in play when the parent can accomodate however big the view is, like the scroll say.

satya - Tue Oct 23 2012 08:53:11 GMT-0400 (Eastern Daylight Time)

Here is a link that talks about scroll and unspecified

Here is a link that talks about scroll and unspecified

satya - Tue Oct 23 2012 08:54:45 GMT-0400 (Eastern Daylight Time)

Here is how you end up measuring your children if you are a layout


//Take from the link above

//for wrap_content
frame.measure(MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST),
        MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST));

//for getting your full size
frame.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

satya - Tue Oct 23 2012 08:55:58 GMT-0400 (Eastern Daylight Time)

The suggestin is from adamp

The suggestin is from adamp

satya - Tue Oct 23 2012 10:30:32 GMT-0400 (Eastern Daylight Time)

How base class measures in the View


protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
    }

satya - Tue Oct 23 2012 10:31:33 GMT-0400 (Eastern Daylight Time)

Here is getDfaultSize implementation


public static int getDefaultSize(int size, int measureSpec) {
        int result = size;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize =  MeasureSpec.getSize(measureSpec);
        
        switch (specMode) {
        case MeasureSpec.UNSPECIFIED:
            result = size;
            break;
        case MeasureSpec.AT_MOST:
        case MeasureSpec.EXACTLY:
            result = specSize;
            break;
        }
        return result;
    }

satya - Tue Oct 23 2012 10:32:16 GMT-0400 (Eastern Daylight Time)

Interesting it is a static public function

Interesting it is a static public function

satya - Tue Oct 23 2012 10:45:45 GMT-0400 (Eastern Daylight Time)

Perhaps something like this would be better


/*
     * Unspecified: 
     *     used for scrolling
     *     means you can be as big as you would like to be
     *     return the maximum comfortable size
     *     it is ok if you are bigger because scrolling may be on
     *     
     * Exact:
     *     You have indicated your explicist size in the layout
     *     or you said match_parent with the parents exact size
     *     return back the passed in size
     *    
     * atmost:
     *       I have this much space to spare
     *     sent when wrap_content
     *     Take as much as you think your natural size is
     *     this is a bit misleading
     *     you are advised not to take all the size
     *     you should be smaller and return a preferred size
     *     if you don't and take all the space, the other siblings
     *     will get lost.
     * 
     */
    public int getImprovedDefaultHeight(int size2, int measureSpec) {
        //int result = size;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize =  MeasureSpec.getSize(measureSpec);
        
        switch (specMode) {
        case MeasureSpec.UNSPECIFIED:
            result = getMaxiumHeight();
            break;
        case MeasureSpec.EXACTLY:
            result = specSize;
            break;
        case MeasureSpec.AT_MOST:
           result = getMinimumHeight();
            break;
        }
        return result;
    }    
    abstract protected int getMaximumHeight();
    abstract protected int getMinimumHeight();
    abstract protected int getMaximumWidth();
    abstract protected int getMinimumWidth();