public static class View.MeasureSpec
extends Object



A MeasureSpec encapsulates the layout requirements passed from parent to child. Each MeasureSpec represents a requirement for either the width or the height. A MeasureSpec is comprised of a size and a mode. There are three possible modes:

UNSPECIFIED
The parent has not imposed any constraint on the child. It can be whatever size it wants.
EXACTLY
The parent has determined an exact size for the child. The child is going to be given those bounds regardless of how big it wants to be.
AT_MOST
The child can be as large as it wants up to the specified size.

MeasureSpecs are implemented as ints to reduce object allocation. This class is provided to pack and unpack the <size, mode> tuple into the int.

Summary

Constants

int AT_MOST

Measure specification mode: The child can be as large as it wants up to the specified size.

int EXACTLY

Measure specification mode: The parent has determined an exact size for the child.

int UNSPECIFIED

Measure specification mode: The parent has not imposed any constraint on the child.

Public constructors

MeasureSpec()

Public methods

static int getMode(int measureSpec)

Extracts the mode from the supplied measure specification.

static int getSize(int measureSpec)

Extracts the size from the supplied measure specification.

static int makeMeasureSpec(int size, int mode)

Creates a measure specification based on the supplied size and mode.

static String toString(int measureSpec)

Returns a String representation of the specified measure specification.

Inherited methods

From class java.lang.Object

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Constants

AT_MOST

public static final int AT_MOST

Measure specification mode: The child can be as large as it wants up to the specified size.

Constant Value: -2147483648 (0x80000000)

EXACTLY

public static final int EXACTLY

Measure specification mode: The parent has determined an exact size for the child. The child is going to be given those bounds regardless of how big it wants to be.

Constant Value: 1073741824 (0x40000000)

UNSPECIFIED

public static final int UNSPECIFIED

Measure specification mode: The parent has not imposed any constraint on the child. It can be whatever size it wants.

Constant Value: 0 (0x00000000)

Public constructors

MeasureSpec

public MeasureSpec ()

Public methods

getMode

public static int getMode (int measureSpec)

Extracts the mode from the supplied measure specification.

Parameters
measureSpec int: the measure specification to extract the mode from
Returns
int UNSPECIFIED, AT_MOST or EXACTLY
Value is one of the following:

getSize

public static int getSize (int measureSpec)

Extracts the size from the supplied measure specification.

Parameters
measureSpec int: the measure specification to extract the size from
Returns
int the size in pixels defined in the supplied measure specification

makeMeasureSpec

public static int makeMeasureSpec (int size, 
                int mode)

Creates a measure specification based on the supplied size and mode. The mode must always be one of the following:

Note: On API level 17 and lower, makeMeasureSpec's implementation was such that the order of arguments did not matter and overflow in either value could impact the resulting MeasureSpec. RelativeLayout was affected by this bug. Apps targeting API levels greater than 17 will get the fixed, more strict behavior.

Parameters
size int: the size of the measure specification.
Value is between 0 and 1073741823 inclusive
mode int: the mode of the measure specification.
Value is one of the following:
Returns
int the measure specification based on size and mode

toString

public static String toString (int measureSpec)

Returns a String representation of the specified measure specification.

Parameters
measureSpec int: the measure specification to convert to a String
Returns
String a String with the following format: "MeasureSpec: MODE SIZE"

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2026-02-13 UTC.