Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)
Android APIs
public class

Gravity

extends Object
java.lang.Object
   ↳ android.view.Gravity

Class Overview

Standard constants and tools for placing an object within a potentially larger container.

Summary

Constants
int AXIS_CLIP Raw bit controlling whether the right/bottom edge is clipped to its container, based on the gravity direction being applied.
int AXIS_PULL_AFTER Raw bit controlling how the right/bottom edge is placed.
int AXIS_PULL_BEFORE Raw bit controlling how the left/top edge is placed.
int AXIS_SPECIFIED Raw bit indicating the gravity for an axis has been specified.
int AXIS_X_SHIFT Bits defining the horizontal axis.
int AXIS_Y_SHIFT Bits defining the vertical axis.
int BOTTOM Push object to the bottom of its container, not changing its size.
int CENTER Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
int CENTER_HORIZONTAL Place object in the horizontal center of its container, not changing its size.
int CENTER_VERTICAL Place object in the vertical center of its container, not changing its size.
int CLIP_HORIZONTAL Flag to clip the edges of the object to its container along the horizontal axis.
int CLIP_VERTICAL Flag to clip the edges of the object to its container along the vertical axis.
int DISPLAY_CLIP_HORIZONTAL Special constant to enable clipping to an overall display along the horizontal dimension.
int DISPLAY_CLIP_VERTICAL Special constant to enable clipping to an overall display along the vertical dimension.
int END Push object to x-axis position at the end of its container, not changing its size.
int FILL Grow the horizontal and vertical size of the object if needed so it completely fills its container.
int FILL_HORIZONTAL Grow the horizontal size of the object if needed so it completely fills its container.
int FILL_VERTICAL Grow the vertical size of the object if needed so it completely fills its container.
int HORIZONTAL_GRAVITY_MASK Binary mask to get the absolute horizontal gravity of a gravity.
int LEFT Push object to the left of its container, not changing its size.
int NO_GRAVITY Constant indicating that no gravity has been set
int RELATIVE_HORIZONTAL_GRAVITY_MASK Binary mask for the horizontal gravity and script specific direction bit.
int RELATIVE_LAYOUT_DIRECTION Raw bit controlling whether the layout direction is relative or not (START/END instead of absolute LEFT/RIGHT).
int RIGHT Push object to the right of its container, not changing its size.
int START Push object to x-axis position at the start of its container, not changing its size.
int TOP Push object to the top of its container, not changing its size.
int VERTICAL_GRAVITY_MASK Binary mask to get the vertical gravity of a gravity.
Public Constructors
Gravity ()
Public Methods
static void apply (int gravity, int w, int h, Rect container, Rect outRect, int layoutDirection)
Apply a gravity constant to an object and take care if layout direction is RTL or not.
static void apply (int gravity, int w, int h, Rect container, int xAdj, int yAdj, Rect outRect)
Apply a gravity constant to an object.
static void apply (int gravity, int w, int h, Rect container, Rect outRect)
Apply a gravity constant to an object.
static void apply (int gravity, int w, int h, Rect container, int xAdj, int yAdj, Rect outRect, int layoutDirection)
Apply a gravity constant to an object.
static void applyDisplay (int gravity, Rect display, Rect inoutObj)
Apply additional gravity behavior based on the overall "display" that an object exists in.
static void applyDisplay (int gravity, Rect display, Rect inoutObj, int layoutDirection)
Apply additional gravity behavior based on the overall "display" that an object exists in.
static int getAbsoluteGravity (int gravity, int layoutDirection)

Convert script specific gravity to absolute horizontal value.

static boolean isHorizontal (int gravity)

Indicate whether the supplied gravity has an horizontal pull.

static boolean isVertical (int gravity)

Indicate whether the supplied gravity has a vertical pull.

[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int AXIS_CLIP

Added in API level 3

Raw bit controlling whether the right/bottom edge is clipped to its container, based on the gravity direction being applied.

Constant Value: 8 (0x00000008)

public static final int AXIS_PULL_AFTER

Added in API level 1

Raw bit controlling how the right/bottom edge is placed.

Constant Value: 4 (0x00000004)

public static final int AXIS_PULL_BEFORE

Added in API level 1

Raw bit controlling how the left/top edge is placed.

Constant Value: 2 (0x00000002)

public static final int AXIS_SPECIFIED

Added in API level 1

Raw bit indicating the gravity for an axis has been specified.

Constant Value: 1 (0x00000001)

public static final int AXIS_X_SHIFT

Added in API level 1

Bits defining the horizontal axis.

Constant Value: 0 (0x00000000)

public static final int AXIS_Y_SHIFT

Added in API level 1

Bits defining the vertical axis.

Constant Value: 4 (0x00000004)

public static final int BOTTOM

Added in API level 1

Push object to the bottom of its container, not changing its size.

Constant Value: 80 (0x00000050)

public static final int CENTER

Added in API level 1

Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.

Constant Value: 17 (0x00000011)

public static final int CENTER_HORIZONTAL

Added in API level 1

Place object in the horizontal center of its container, not changing its size.

Constant Value: 1 (0x00000001)

public static final int CENTER_VERTICAL

Added in API level 1

Place object in the vertical center of its container, not changing its size.

Constant Value: 16 (0x00000010)

public static final int CLIP_HORIZONTAL

Added in API level 3

Flag to clip the edges of the object to its container along the horizontal axis.

Constant Value: 8 (0x00000008)

public static final int CLIP_VERTICAL

Added in API level 3

Flag to clip the edges of the object to its container along the vertical axis.

Constant Value: 128 (0x00000080)

public static final int DISPLAY_CLIP_HORIZONTAL

Added in API level 3

Special constant to enable clipping to an overall display along the horizontal dimension. This is not applied by default by apply(int, int, int, Rect, int, int, Rect) ; you must do so yourself by calling applyDisplay(int, Rect, Rect) .

Constant Value: 16777216 (0x01000000)

public static final int DISPLAY_CLIP_VERTICAL

Added in API level 3

Special constant to enable clipping to an overall display along the vertical dimension. This is not applied by default by apply(int, int, int, Rect, int, int, Rect) ; you must do so yourself by calling applyDisplay(int, Rect, Rect) .

Constant Value: 268435456 (0x10000000)

public static final int END

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

Constant Value: 8388613 (0x00800005)

public static final int FILL

Added in API level 1

Grow the horizontal and vertical size of the object if needed so it completely fills its container.

Constant Value: 119 (0x00000077)

public static final int FILL_HORIZONTAL

Added in API level 1

Grow the horizontal size of the object if needed so it completely fills its container.

Constant Value: 7 (0x00000007)

public static final int FILL_VERTICAL

Added in API level 1

Grow the vertical size of the object if needed so it completely fills its container.

Constant Value: 112 (0x00000070)

public static final int HORIZONTAL_GRAVITY_MASK

Added in API level 1

Binary mask to get the absolute horizontal gravity of a gravity.

Constant Value: 7 (0x00000007)

public static final int LEFT

Added in API level 1

Push object to the left of its container, not changing its size.

Constant Value: 3 (0x00000003)

public static final int NO_GRAVITY

Added in API level 1

Constant indicating that no gravity has been set

Constant Value: 0 (0x00000000)

public static final int RELATIVE_HORIZONTAL_GRAVITY_MASK

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

Constant Value: 8388615 (0x00800007)

public static final int RELATIVE_LAYOUT_DIRECTION

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

Constant Value: 8388608 (0x00800000)

public static final int RIGHT

Added in API level 1

Push object to the right of its container, not changing its size.

Constant Value: 5 (0x00000005)

public static final int START

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

Constant Value: 8388611 (0x00800003)

public static final int TOP

Added in API level 1

Push object to the top of its container, not changing its size.

Constant Value: 48 (0x00000030)

public static final int VERTICAL_GRAVITY_MASK

Added in API level 1

Binary mask to get the vertical gravity of a gravity.

Constant Value: 112 (0x00000070)

Public Constructors

public Gravity ()

Added in API level 1

Public Methods

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

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

Parameters
gravity The desired placement of the object, as defined by the constants in this class.
w The horizontal size of the object.
h The vertical size of the object.
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.
outRect Receives the computed frame of the object in its container.
layoutDirection The layout direction.

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

Added in API level 1

Apply a gravity constant to an object.

Parameters
gravity The desired placement of the object, as defined by the constants in this class.
w The horizontal size of the object.
h The vertical size of the object.
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.
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.
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.
outRect Receives the computed frame of the object in its container.

public static void apply (int gravity, int w, int h, Rect container, Rect outRect)

Added in API level 1

Apply a gravity constant to an object. This supposes that the layout direction is LTR.

Parameters
gravity The desired placement of the object, as defined by the constants in this class.
w The horizontal size of the object.
h The vertical size of the object.
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.
outRect Receives the computed frame of the object in its container.

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

Apply a gravity constant to an object.

Parameters
gravity The desired placement of the object, as defined by the constants in this class.
w The horizontal size of the object.
h The vertical size of the object.
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.
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.
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.
outRect Receives the computed frame of the object in its container.
layoutDirection The layout direction.

public static void applyDisplay (int gravity, Rect display, Rect inoutObj)

Added in API level 3

Apply additional gravity behavior based on the overall "display" that an object exists in. This can be used after apply(int, int, int, Rect, int, int, 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 DISPLAY_CLIP_HORIZONTAL and DISPLAY_CLIP_VERTICAL can be used to change this behavior.

Parameters
gravity Gravity constants to modify the placement within the display.
display The rectangle of the display in which the object is being placed.
inoutObj Supplies the current object position; returns with it modified if needed to fit in the display.

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

Apply additional gravity behavior based on the overall "display" that an object exists in. This can be used after apply(int, int, int, Rect, int, int, 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 DISPLAY_CLIP_HORIZONTAL and DISPLAY_CLIP_VERTICAL can be used to change this behavior.

Parameters
gravity Gravity constants to modify the placement within the display.
display The rectangle of the display in which the object is being placed.
inoutObj Supplies the current object position; returns with it modified if needed to fit in the display.
layoutDirection The layout direction.

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
gravity The gravity to convert to absolute (horizontal) values.
layoutDirection The layout direction.
Returns
  • gravity converted to absolute (horizontal) values.

public static boolean isHorizontal (int gravity)

Added in API level 1

Indicate whether the supplied gravity has an horizontal pull.

Parameters
gravity the gravity to check for horizontal pull
Returns
  • true if the supplied gravity has an horizontal pull

public static boolean isVertical (int gravity)

Added in API level 1

Indicate whether the supplied gravity has a vertical pull.

Parameters
gravity the gravity to check for vertical pull
Returns
  • true if the supplied gravity has a vertical pull