On MeasureSpec

MeasureSpec unspecified at_most exact

Search for: MeasureSpec unspecified at_most exact

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.

Here is a link that talks about scroll and unspecified


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

The suggestin is from adamp


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

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

Interesting it is a static public function


/*
     * 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();