SurfaceView onMeasure() did not set the measured dimension by calling setMeasuredDimension()


I have a SurfaceView. I just draw on it manually from an other Thread.

It runs nicely. But after 10-20 minutes of running in average I get this exception:

01-14 08:51:25.000    3740-3740/com.myPackage E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: onMeasure() did not set the measured dimension by calling setMeasuredDimension()
            at android.view.View.measure(
            at android.widget.RelativeLayout.measureChild(
            at android.widget.RelativeLayout.onMeasure(
            at android.view.View.measure(
            at android.view.ViewGroup.measureChildWithMargins(
            at android.widget.FrameLayout.onMeasure(
            at android.view.View.measure(
            at android.view.ViewGroup.measureChildWithMargins(
            at android.widget.LinearLayout.measureChildBeforeLayout(
            at android.widget.LinearLayout.measureVertical(
            at android.widget.LinearLayout.onMeasure(
            at android.view.View.measure(
            at android.view.ViewGroup.measureChildWithMargins(
            at android.widget.FrameLayout.onMeasure(
            at android.view.View.measure(
            at android.view.ViewRootImpl.performMeasure(
            at android.view.ViewRootImpl.measureHierarchy(
            at android.view.ViewRootImpl.performTraversals(
            at android.view.ViewRootImpl.doTraversal(
            at android.view.ViewRootImpl$
            at android.view.Choreographer$
            at android.view.Choreographer.doCallbacks(
            at android.view.Choreographer.doFrame(
            at android.view.Choreographer$
            at android.os.Handler.handleCallback(
            at android.os.Handler.dispatchMessage(
            at android.os.Looper.loop(
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(
            at dalvik.system.NativeStart.main(Native Method)

Android Documention says:
Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overriden by subclasses to provide accurate and efficient measurement of their contents.

As I think the original implementation from google is this:

    protected void More ...onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = getDefaultSize(mRequestedWidth, widthMeasureSpec);
        int height = getDefaultSize(mRequestedHeight, heightMeasureSpec);
        setMeasuredDimension(width, height);

I don't know what I else should I implement here.

I think the size of the surface view never changes.

My SurfaceView is contained in a RelativeLayout.
The exception is called by a child, but it's a view (line 3 in the exception)

Please help me if you know how could I solve this annoying bug.

In Activity's onCreate I set the size of my view (that extended form SurfaceView) from code

Answear to comment:
This is how I set some public fields for the required size:


The Parameters.initialize:

public static void initialize(int screenWidth, int screenHeight){
    FIELD_WIDTH = screenWidth;
    FIELD_HEIGHT = (int)(0.92f * screenHeight);

MyView is written in the xml.
Then I set something on it:

public class MyViewManager {
    protected MyView mMyView;

    public FieldPainterComp(MainActivity activity) {

    private void initialize(){
        mMyView= (MyView) activity.findViewById(;
        mMyView.getLayoutParams().height = Parameters.FIELD_HEIGHT;
        mMyView.getLayoutParams().width = Parameters.FIELD_WIDTH;

This whole process happens ones in the beginning.


Screen never goes dark. Game is running, and simply the exception is coming.
Should I call measure somewhere?
I don't understand why is it happaning after a long time

Some other exception that must be in connected with this:

01-19 11:53:39.812  29666-31881/com.mypackage V/GAV4﹕ Thread[disconnect check,5,main]: Disconnecting due to inactivity
01-19 11:53:39.812  29666-31881/com.mypackage V/GAV4﹕ Thread[disconnect check,5,main]: Disconnected from service
01-19 11:57:17.732  29666-29666/com.mypackage D/AndroidRuntime﹕ Shutting down VM
01-19 11:57:17.732  29666-29666/com.mypackage W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x420face0)
01-19 11:57:17.812  29666-29666/com.mypackage V/GAV4﹕ Thread[main,5,main]: Tracking Exception: IllegalStateException (@View:measure:16533) {main}
01-19 11:57:17.832  29666-29666/com.mypackage V/GAV4﹕ Thread[main,5,main]: Dispatch call queued. Dispatch will run once initialization is complete.
01-19 12:19:27.982  29666-29670/com.mypackage I/dalvikvm﹕ threadid=3: reacting to signal 3
01-19 12:19:28.262  29666-29670/com.mypackage I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
01-19 12:19:32.452  29666-29666/com.mypackage A/libc﹕ Fatal signal 6 (SIGABRT) at 0x0000025a (code=0), thread 29666 (ames.adaptation)

The GAV -is the Google Analytics stuff.

I launched an other question about the ANR: ANR with 3 threads - using locks

asked on Stack Overflow Jan 19, 2015 by Tomi • edited May 23, 2017 by Community

4 Answers


Must be called inside of your onMeasure() method. I had a similar problem where the logic wasn't reaching setMeasuredDimension(). I resolved it forcing the call of setMeasuredDimension() no matter what the if/else statements preceded it.

answered on Stack Overflow Nov 8, 2015 by IgorGanapolsky

You use Fragment and don't write this line at the first

answered on Stack Overflow Jan 9, 2017 by Moeen Kashisaz

private double aspectRatio = 1.0;

private boolean isCalledRecreate = true;

in onMeasure ::- double localRatio = aspectRatio;

    if (localRatio == 0.0) {
        super.onMeasure(widthSpec, heightSpec);

    } else {
        int lockedWidth = MeasureSpec.getSize(widthSpec);
        int lockedHeight = MeasureSpec.getSize(heightSpec);

        if (lockedWidth == 0 && lockedHeight == 0) {

            widthSpec = MeasureSpec.makeMeasureSpec((int) (basicWidth), MeasureSpec.AT_MOST);
            heightSpec = MeasureSpec.makeMeasureSpec((int) (basicHeight), MeasureSpec.AT_MOST);

            lockedWidth = MeasureSpec.getSize(widthSpec);
            lockedHeight = MeasureSpec.getSize(heightSpec);

            Log.i(TAG, "onMeasure: onMeasure");


        // Get the padding of the border background.
        int hPadding = getPaddingLeft() + getPaddingRight();
        int vPadding = getPaddingTop() + getPaddingBottom();

        // Resize the preview frame with correct aspect ratio.
        lockedWidth -= hPadding;
        lockedHeight -= vPadding;

        if (lockedHeight > 0 && (lockedWidth > lockedHeight * localRatio)) {
            lockedWidth = (int) (lockedHeight * localRatio + .5);
        } else {
            lockedHeight = (int) (lockedWidth / localRatio + .5);

        // Add the padding of the border.
        lockedWidth += hPadding;
        lockedHeight += vPadding;

        // Ask children to follow the new preview dimension.
        super.onMeasure(MeasureSpec.makeMeasureSpec(lockedWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(lockedHeight, MeasureSpec.EXACTLY));

Note :- callForRecreate() in that you have to create or restart activity

answered on Stack Overflow Jun 11, 2019 by Bhaven Shah

Make sure to call super.onMeasure(widthMeasureSpec,heightMeasureSpec); //e.g @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec,heightMeasureSpec); //then your code }

answered on Stack Overflow Sep 30, 2020 by Rohit Patil

User contributions licensed under CC BY-SA 3.0