# GravityCompat

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/view/GravityCompat.java+class:androidx.core.view.GravityCompat) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

*** ** * ** ***


```
public final class GravityCompat
```

<br />

*** ** * ** ***

Compatibility shim for accessing newer functionality from `https://developer.android.com/reference/android/view/Gravity.html`.

## Summary

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/core/view/GravityCompat#END() = 8388613` Push object to x-axis position at the end of its container, not changing its size. |
| `static final int` | `https://developer.android.com/reference/androidx/core/view/GravityCompat#RELATIVE_HORIZONTAL_GRAVITY_MASK() = 8388615` Binary mask for the horizontal gravity and script specific direction bit. |
| `static final int` | `https://developer.android.com/reference/androidx/core/view/GravityCompat#RELATIVE_LAYOUT_DIRECTION() = 8388608` Raw bit controlling whether the layout direction is relative or not (START/END instead of absolute LEFT/RIGHT). |
| `static final int` | `https://developer.android.com/reference/androidx/core/view/GravityCompat#START() = 8388611` Push object to x-axis position at the start of its container, not changing its size. |

| ### Public methods |
|---|---|
| `static void` | `https://developer.android.com/reference/androidx/core/view/GravityCompat#apply(int,int,int,android.graphics.Rect,android.graphics.Rect,int)( int gravity, int w, int h, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Rect.html container, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Rect.html outRect, int layoutDirection )` Apply a gravity constant to an object and take care if layout direction is RTL or not. |
| `static void` | `https://developer.android.com/reference/androidx/core/view/GravityCompat#apply(int,int,int,android.graphics.Rect,int,int,android.graphics.Rect,int)( int gravity, int w, int h, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Rect.html container, int xAdj, int yAdj, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Rect.html outRect, int layoutDirection )` Apply a gravity constant to an object. |
| `static void` | `https://developer.android.com/reference/androidx/core/view/GravityCompat#applyDisplay(int,android.graphics.Rect,android.graphics.Rect,int)( int gravity, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Rect.html display, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Rect.html inoutObj, int layoutDirection )` Apply additional gravity behavior based on the overall "display" that an object exists in. |
| `static int` | `https://developer.android.com/reference/androidx/core/view/GravityCompat#getAbsoluteGravity(int,int)(int gravity, int layoutDirection)` Convert script specific gravity to absolute horizontal value. |

## Constants

### END

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final int END = 8388613
```

Push object to x-axis position at the end of its container, not changing its size.

### RELATIVE_HORIZONTAL_GRAVITY_MASK

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final int RELATIVE_HORIZONTAL_GRAVITY_MASK = 8388615
```

Binary mask for the horizontal gravity and script specific direction bit.

### RELATIVE_LAYOUT_DIRECTION

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final int RELATIVE_LAYOUT_DIRECTION = 8388608
```

Raw bit controlling whether the layout direction is relative or not (START/END instead of absolute LEFT/RIGHT).

### START

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final int START = 8388611
```

Push object to x-axis position at the start of its container, not changing its size.

## Public methods

### apply

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static void apply(
    int gravity,
    int w,
    int h,
    @NonNull Rect container,
    @NonNull Rect outRect,
    int layoutDirection
)
```

Apply a gravity constant to an object and take care if layout direction is RTL or not.

| Parameters |
|---|---|
| `int gravity` | The desired placement of the object, as defined by the constants in this class. |
| `int w` | The horizontal size of the object. |
| `int h` | The vertical size of the object. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Rect.html container` | The frame of the containing space, in which the object will be placed. Should be large enough to contain the width and height of the object. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Rect.html outRect` | Receives the computed frame of the object in its container. |
| `int layoutDirection` | The layout direction. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/core/view/ViewCompat#LAYOUT_DIRECTION_LTR()` |   |
| `https://developer.android.com/reference/androidx/core/view/ViewCompat#LAYOUT_DIRECTION_RTL()` |   |

### apply

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static void apply(
    int gravity,
    int w,
    int h,
    @NonNull Rect container,
    int xAdj,
    int yAdj,
    @NonNull Rect outRect,
    int layoutDirection
)
```

Apply a gravity constant to an object.

| Parameters |
|---|---|
| `int gravity` | The desired placement of the object, as defined by the constants in this class. |
| `int w` | The horizontal size of the object. |
| `int h` | The vertical size of the object. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Rect.html container` | The frame of the containing space, in which the object will be placed. Should be large enough to contain the width and height of the object. |
| `int xAdj` | Offset to apply to the X axis. If gravity is LEFT this pushes it to the right; if gravity is RIGHT it pushes it to the left; if gravity is CENTER_HORIZONTAL it pushes it to the right or left; otherwise it is ignored. |
| `int yAdj` | Offset to apply to the Y axis. If gravity is TOP this pushes it down; if gravity is BOTTOM it pushes it up; if gravity is CENTER_VERTICAL it pushes it down or up; otherwise it is ignored. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Rect.html outRect` | Receives the computed frame of the object in its container. |
| `int layoutDirection` | The layout direction. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/core/view/ViewCompat#LAYOUT_DIRECTION_LTR()` |   |
| `https://developer.android.com/reference/androidx/core/view/ViewCompat#LAYOUT_DIRECTION_RTL()` |   |

### applyDisplay

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static void applyDisplay(
    int gravity,
    @NonNull Rect display,
    @NonNull Rect inoutObj,
    int layoutDirection
)
```

Apply additional gravity behavior based on the overall "display" that an object exists in. This can be used after `https://developer.android.com/reference/android/view/Gravity.html#apply(int, int, int, android.graphics.Rect, int, int, android.graphics.Rect)` to place the object within a visible display. By default this moves or clips the object to be visible in the display; the gravity flags `https://developer.android.com/reference/android/view/Gravity.html#DISPLAY_CLIP_HORIZONTAL` and `https://developer.android.com/reference/android/view/Gravity.html#DISPLAY_CLIP_VERTICAL` can be used to change this behavior.

| Parameters |
|---|---|
| `int gravity` | Gravity constants to modify the placement within the display. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Rect.html display` | The rectangle of the display in which the object is being placed. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Rect.html inoutObj` | Supplies the current object position; returns with it modified if needed to fit in the display. |
| `int layoutDirection` | The layout direction. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/core/view/ViewCompat#LAYOUT_DIRECTION_LTR()` |   |
| `https://developer.android.com/reference/androidx/core/view/ViewCompat#LAYOUT_DIRECTION_RTL()` |   |

### getAbsoluteGravity

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static int getAbsoluteGravity(int gravity, int layoutDirection)
```

Convert script specific gravity to absolute horizontal value.
if horizontal direction is LTR, then START will set LEFT and END will set RIGHT. if horizontal direction is RTL, then START will set RIGHT and END will set LEFT.

| Parameters |
|---|---|
| `int gravity` | The gravity to convert to absolute (horizontal) values. |
| `int layoutDirection` | The layout direction. |

| Returns |
|---|---|
| `int` | gravity converted to absolute (horizontal) values. |