# View.MeasureSpec

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

# MeasureSpec

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/android/view/View.MeasureSpec "View this page in Java")  

```
open class MeasureSpec
```

|---|----------------------------------------------------------------------------------------------------------------|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)                                 ||
| ↳ | [android.view.View.MeasureSpec](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#) |

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.

<br />

## Summary

|                                                                                                                                        Constants                                                                                                                                         ||
|----------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| static[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | [AT_MOST](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#AT_MOST:kotlin.Int) Measure specification mode: The child can be as large as it wants up to the specified size. |
| static[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | [EXACTLY](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#EXACTLY:kotlin.Int) Measure specification mode: The parent has determined an exact size for the child.          |
| static[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | [UNSPECIFIED](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#UNSPECIFIED:kotlin.Int) Measure specification mode: The parent has not imposed any constraint on the child. |

|                                                   Public constructors                                                   ||
|----------------------------------------------------------------------------------------------------------------------|---|
| [MeasureSpec](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#MeasureSpec())`()` <br /> |

|                                                                                                                                                                                                                                      Public methods                                                                                                                                                                                                                                       ||
|---------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| openstatic[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)        | [getMode](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#getMode(kotlin.Int))`(`measureSpec:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Extracts the mode from the supplied measure specification.                                                                                                                        |
| openstatic[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)        | [getSize](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#getSize(kotlin.Int))`(`measureSpec:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Extracts the size from the supplied measure specification.                                                                                                                        |
| openstatic[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)        | [makeMeasureSpec](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#makeMeasureSpec(kotlin.Int,%20kotlin.Int))`(`size:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`, `mode:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Creates a measure specification based on the supplied size and mode. |
| openstatic[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)! | [toString](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#toString(kotlin.Int))`(`measureSpec:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Returns a String representation of the specified measure specification.                                                                                                         |

## Constants

### AT_MOST

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
static val AT_MOST: Int
```

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

    Value: -2147483648

### EXACTLY

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
static val EXACTLY: Int
```

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.  

    Value: 1073741824

### UNSPECIFIED

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
static val UNSPECIFIED: Int
```

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

    Value: 0

## Public constructors

### MeasureSpec

```
MeasureSpec()
```

## Public methods

### getMode

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open static fun getMode(measureSpec: Int): Int
```

Extracts the mode from the supplied measure specification.

|                                                                  Parameters                                                                  ||
|---------------|-------------------------------------------------------------------------------------------------------------------------------|
| `measureSpec` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html):the measure specification to extract the mode from |

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                            Return                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ||
|----------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | [android.view.View.MeasureSpec#UNSPECIFIED](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#UNSPECIFIED:kotlin.Int),[android.view.View.MeasureSpec#AT_MOST](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#AT_MOST:kotlin.Int)or[android.view.View.MeasureSpec#EXACTLY](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#EXACTLY:kotlin.Int)Value is[android.view.View.MeasureSpec#UNSPECIFIED](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#UNSPECIFIED:kotlin.Int),[android.view.View.MeasureSpec#EXACTLY](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#EXACTLY:kotlin.Int), or[android.view.View.MeasureSpec#AT_MOST](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#AT_MOST:kotlin.Int) |

### getSize

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open static fun getSize(measureSpec: Int): Int
```

Extracts the size from the supplied measure specification.

|                                                                  Parameters                                                                  ||
|---------------|-------------------------------------------------------------------------------------------------------------------------------|
| `measureSpec` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html):the measure specification to extract the size from |

|                                                                    Return                                                                    ||
|----------------------------------------------------------------------------|------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | the size in pixels defined in the supplied measure specification |

### makeMeasureSpec

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open static fun makeMeasureSpec(
    size: Int, 
    mode: Int
): Int
```

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

- [android.view.View.MeasureSpec#UNSPECIFIED](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#UNSPECIFIED:kotlin.Int)
- [android.view.View.MeasureSpec#EXACTLY](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#EXACTLY:kotlin.Int)
- [android.view.View.MeasureSpec#AT_MOST](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#AT_MOST:kotlin.Int)

<br />

**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.[android.widget.RelativeLayout](https://developer.android.com/reference/kotlin/android/widget/RelativeLayout.html#)was affected by this bug. Apps targeting API levels greater than 17 will get the fixed, more strict behavior.

|                                                                                                                                                                                                                                                                               Parameters                                                                                                                                                                                                                                                                               ||
|--------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `size` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html):the size of the measure specification Value is between 0 and 1073741823 inclusive                                                                                                                                                                                                                                                                                                                                                                                                   |
| `mode` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html):the mode of the measure specification Value is[android.view.View.MeasureSpec#UNSPECIFIED](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#UNSPECIFIED:kotlin.Int),[android.view.View.MeasureSpec#EXACTLY](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#EXACTLY:kotlin.Int), or[android.view.View.MeasureSpec#AT_MOST](https://developer.android.com/reference/kotlin/android/view/View.MeasureSpec#AT_MOST:kotlin.Int) |

|                                                            Return                                                            ||
|----------------------------------------------------------------------------|--------------------------------------------------|
| [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | the measure specification based on size and mode |

### toString

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open static fun toString(measureSpec: Int): String!
```

Returns a String representation of the specified measure specification.

|                                                                 Parameters                                                                 ||
|---------------|-----------------------------------------------------------------------------------------------------------------------------|
| `measureSpec` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html):the measure specification to convert to a String |

|                                                                     Return                                                                      ||
|-----------------------------------------------------------------------------------|--------------------------------------------------------------|
| [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)! | a String with the following format: "MeasureSpec: MODE SIZE" |