public class MotionLayout extends ConstraintLayout implements NestedScrollingParent3


A subclass of ConstraintLayout that supports animating between various states Added in 2.0

A MotionLayout is a subclass of ConstraintLayout which supports transitions between between various states (ConstraintSet) defined in MotionScenes.

Note:MotionLayout is available as a support library that you can use on Android systems starting with API level 14 (ICS).

MotionLayout links to and requires a MotionScene file. The file contains one top level tag "MotionScene"

LayoutDescription

Tags Description
<StateSet> Describes states supported by the system (optional)
<ConstraintSet> Describes a constraint set
<Transition> Describes a transition between two states or ConstraintSets
<ViewTransition> Describes a transition of a View within a states or ConstraintSets

Transition

Attributes Description
android:id The id of the Transition
constraintSetStart ConstraintSet to be used as the start constraints or a layout file to get the constraint from
constraintSetEnd ConstraintSet to be used as the end constraints or a layout file to get the constraint from
motionInterpolator The ability to set an overall interpolation (easeInOut, linear, etc.)
duration Length of time to take to perform the transition
staggered Overrides the Manhattan distance from the top most view in the list of views.
  • For any view of stagger value S(Vi)
  • With the transition stagger value of TS (from 0.0 - 1.0)
  • The duration of the animation is duration
  • The views animation duration DS = duration * (1 -TS)
  • Call the stagger fraction SFi = (S(Vi) - S(V0)) / (S(Vn) - S(V0))
  • The view starts animating at: (duration-DS) * SFi
pathMotionArc The path will move in arc (quarter ellipses) key words {startVertical | startHorizontal | flip | none }
autoTransition automatically transition from one state to another. key words {none, jumpToStart, jumpToEnd, animateToStart, animateToEnd}
transitionFlags flags that adjust the behaviour of Transitions. supports {none, beginOnFirstDraw} begin on first draw forces the transition's clock to start when it is first displayed not when the begin is called
layoutDuringTransition Configures MotionLayout on how to react to requestLayout calls during transitions. Allowed values are {ignoreRequest, honorRequest}
<OnSwipe> Adds support for touch handling (optional)
<OnClick> Adds support for triggering transition (optional)
<KeyFrameSet> Describes a set of Key object which modify the animation between constraint sets.
  • A transition is typically defined by specifying its start and end ConstraintSets. You also have the possibility to not specify them, in which case such transition will become a Default transition. That Default transition will be applied between any state change that isn't explicitly covered by a transition.
  • The starting state of the MotionLayout is defined to be the constraintSetStart of the first transition.
  • If no transition is specified (or only a default Transition) the MotionLayout tag must contain a app:currentState to define the starting state of the MotionLayout

ViewTransition

Attributes Description
android:id The id of the ViewTransition
viewTransitionMode currentState, allStates, noState transition affect the state of the view in the current constraintSet or all ConstraintSets or non if noState the ViewTransitions are run asynchronous
onStateTransition actionDown or actionUp run transition if on touch down or up if view matches motionTarget
motionInterpolator The ability to set an overall interpolation key words {easeInOut, linear, etc.}
duration Length of time to take to perform the ViewTransition
pathMotionArc The path will move in arc (quarter ellipses) key words {startVertical | startHorizontal | flip | none }
motionTarget Apply ViewTransition matching this string or id.
setsTag set this tag at end of transition
clearsTag clears this tag at end of transition
ifTagSet run transition if this tag is set on view
ifTagNotSet run transition if this tag is not set on view/td>
<OnSwipe> Adds support for touch handling (optional)
<OnClick> Adds support for triggering transition (optional)
<KeyFrameSet> Describes a set of Key object which modify the animation between constraint sets.
  • A Transition is typically defined by specifying its start and end ConstraintSets. You also have the possibility to not specify them, in which case such transition will become a Default transition. That Default transition will be applied between any state change that isn't explicitly covered by a transition.
  • The starting state of the MotionLayout is defined to be the constraintSetStart of the first transition.
  • If no transition is specified (or only a default Transition) the MotionLayout tag must contain a app:currentState to define the starting state of the MotionLayout

OnSwipe (optional)

Attributes Description
touchAnchorId Have the drag act as if it is moving the "touchAnchorSide" of this object
touchRegionId Limits the region that the touch can be start in to the bounds of this view (even if the view is invisible)
touchAnchorSide The side of the object to move with {top|left|right|bottom}
maxVelocity limit the maximum velocity (in progress/sec) of the animation will on touch up. Default 4
dragDirection which side to swipe from {dragUp|dragDown|dragLeft|dragRight}
maxAcceleration how quickly the animation will accelerate (progress/sec/sec) and decelerate on touch up. Default 1.2
dragScale scale factor to adjust the swipe by. (e.g. 0.5 would require you to move 2x as much)
dragThreshold How much to drag before swipe gesture runs. Important for mult-direction swipe. Default is 10. 1 is very sensitive.
moveWhenScrollAtTop If the swipe is scrolling and View (such as RecyclerView or NestedScrollView) do scroll and transition happen at the same time
onTouchUp Support for various swipe modes autoComplete,autoCompleteToStart,autoCompleteToEnd,stop,decelerate,decelerateAndComplete

OnClick (optional)

Attributes Description
motionTarget What view triggers Transition.
clickAction Direction for buttons to move the animation. Or (|) combination of: toggle, transitionToEnd, transitionToStart, jumpToEnd, jumpToStart

StateSet

defaultState The constraint set or layout to use
<State> The side of the object to move

State

android:id Id of the State
constraints Id of the ConstraintSet or the Layout file
<Variant> a different constraintSet/layout to choose if the with or height matches

Variant

region_widthLessThan Match if width less than
region_widthMoreThan Match if width more than
region_heightLessThan Match if height less than
region_heightMoreThan Match if height more than
constraints Id of the ConstraintSet or layout

ConstraintSet

android:id The id of the ConstraintSet
deriveConstraintsFrom The id of another constraintSet which defines the constraints not define in this set. If not specified the layout defines the undefined constraints.
<Constraint> A ConstraintLayout Constraints + other attributes associated with a view

Constraint

Constraint supports two forms:

1: All of ConstraintLayout + the ones listed below + <CustomAttribute> .

Or

2: Combination of tags: <Layout> <PropertySet> <Transform> <Motion> <CustomAttribute> . The advantage of using these is that if not present the attributes are taken from the base layout file. This saves from replicating all the layout tags if only a Motion tag is needed. If is used then all layout attributes in the base are ignored.

android:id Id of the View
[ConstraintLayout attributes] Any attribute that is part of ConstraintLayout layout is allowed
[Standard View attributes] A collection of view attributes supported by the system (see below)
transitionEasing define an easing curve to be used when animating from this point (e.g. curve(1.0,0,0,1.0)) or key words {standard | accelerate | decelerate | linear}
pathMotionArc the path will move in arc (quarter ellipses) or key words {startVertical | startHorizontal | none }
transitionPathRotate (float) rotate object relative to path taken
drawPath draw the path the layout will animate animate
progress call method setProgress(float) on this view (used to talk to nested ConstraintLayouts etc.)
<CustomAttribute> call a set"name" method via reflection
<Layout> Attributes for the ConstraintLayout e.g. layout_constraintTop_toTopOf
<PropertySet> currently only visibility, alpha, motionProgress,layout_constraintTag.
<Transform> All the view transform API such as android:rotation.
<Motion> Motion Layout control commands such as transitionEasing and pathMotionArc

Layout

[ConstraintLayout attributes] Also see ConstraintLayout.LayoutParams for attributes

PropertySet

visibility set the Visibility of the view. One of Visible, invisible or gone
alpha setAlpha value
motionProgress using reflection call setProgress
layout_constraintTag a tagging string to identify the type of object

Transform

android:elevation base z depth of the view.
android:rotation rotation of the view, in degrees.
android:rotationX rotation of the view around the x axis, in degrees.
android:rotationY rotation of the view around the y axis, in degrees.
android:scaleX scale of the view in the x direction
android:scaleY scale of the view in the y direction.
android:translationX translation in x of the view. This value is added post-layout to the left property of the view, which is set by its layout.
android:translationY translation in y of the view. This value is added post-layout to th e top property of the view, which is set by its layout
android:translationZ translation in z of the view. This value is added to its elevation.

Motion

transitionEasing Defines an acceleration curve.
pathMotionArc Says the object should move in a quarter ellipse unless the motion is vertical or horizontal
motionPathRotate set the rotation to the path of the object + this angle.
drawPath Debugging utility to draw the motion of the path

CustomAttribute

attributeName The name of the attribute. Case sensitive. ( MyAttr will look for method setMyAttr(...)
customColorValue The value is a color looking setMyAttr(int )
customIntegerValue The value is an integer looking setMyAttr(int )
customFloatValue The value is a float looking setMyAttr(float )
customStringValue The value is a String looking setMyAttr(String )
customDimension The value is a dimension looking setMyAttr(float )
customBoolean The value is true or false looking setMyAttr(boolean )

KeyFrameSet

This is the container for a collection of Key objects (such as KeyPosition) which provide information about how the views should move

<KeyPosition> Controls the layout position during animation
<KeyAttribute> Controls the post layout properties during animation
<KeyCycle> Controls oscillations with respect to position of post layout properties during animation
<KeyTimeCycle> Controls oscillations with respect to time of post layout properties during animation
<KeyTrigger> trigger callbacks into code at fixed point during the animation

KeyPosition

motionTarget Id of the View or a regular expression to match layout_ConstraintTag
framePosition The point along the interpolation 0 = start 100 = end
transitionEasing define an easing curve to be used when animating from this point (e.g. curve(1.0,0,0, 1.0)) or key words {standard | accelerate | decelerate | linear }
pathMotionArc The path will move in arc (quarter ellipses) key words {startVertical | startHorizontal | flip | none }
keyPositionType how this keyframe's deviation for linear path is calculated {deltaRelative | pathRelative|parentRelative}
percentX (float) percent distance from start to end along X axis (deltaRelative) or along the path in pathRelative
percentY (float) Percent distance from start to end along Y axis (deltaRelative) or perpendicular to path in pathRelative
percentWidth (float) Percent of change in the width. Note if the width does not change this has no effect.This overrides sizePercent.
percentHeight (float) Percent of change in the width. Note if the width does not change this has no effect.This overrides sizePercent.
curveFit path is traced
drawPath Draw the path of the objects layout takes useful for debugging
sizePercent If the view changes size this controls how growth of the size. (for fixed size objects use KeyAttributes scaleX/X)
curveFit selects a path based on straight lines or a path based on a monotonic spline {linear|spline}

KeyAttribute

motionTarget Id of the View or a regular expression to match layout_ConstraintTag
framePosition The point along the interpolation 0 = start 100 = end
curveFit selects a path based on straight lines or a path based on a monotonic spline {linear|spline}
transitionEasing Define an easing curve to be used when animating from this point (e.g. curve(1.0,0,0, 1.0)) or key words {standard | accelerate | decelerate | linear }
transitionPathRotate (float) rotate object relative to path taken
drawPath draw the path the layout will animate animate
motionProgress call method setProgress(float) on this view (used to talk to nested ConstraintLayouts etc.)
[standard view attributes](except visibility) A collection of post layout view attributes see below
<CustomAttribute> call a set"name" method via reflection

CustomAttribute

attributeName The name of the attribute. Case sensitive. ( MyAttr will look for method setMyAttr(...)
customColorValue The value is a color looking setMyAttr(int )
customIntegerValue The value is an integer looking setMyAttr(int )
customFloatValue The value is a float looking setMyAttr(float )
customStringValue The value is a String looking setMyAttr(String )
customDimension The value is a dimension looking setMyAttr(float )
customBoolean The value is true or false looking setMyAttr(boolean )

KeyCycle

motionTarget Id of the View or a regular expression to match layout_ConstraintTag
framePosition The point along the interpolation 0 = start 100 = end
[Standard View attributes] A collection of view attributes supported by the system (see below)
waveShape The shape of the wave to generate {sin|square|triangle|sawtooth|reverseSawtooth|cos|bounce}
wavePeriod The number of cycles to loop near this region
waveOffset offset value added to the attribute
transitionPathRotate Cycles applied to rotation relative to the path the view is travelling
progress call method setProgress(float) on this view (used to talk to nested ConstraintLayouts etc.)
<CustomAttribute> call a set"name" method via reflection (limited to floats)

CustomAttribute

attributeName The name of the attribute. Case sensitive. ( MyAttr will look for method setMyAttr(...)
customFloatValue The value is a float looking setMyAttr(float )

KeyTimeCycle

motionTarget Id of the View or a regular expression to match layout_ConstraintTag
framePosition The point along the interpolation 0 = start 100 = end
[Standard View attributes] A collection of view attributes supported by the system (see below)
waveShape The shape of the wave to generate {sin|square|triangle|sawtooth|reverseSawtooth|cos|bounce}
wavePeriod The number of cycles per second
waveOffset offset value added to the attribute
transitionPathRotate Cycles applied to rotation relative to the path the view is travelling
progress call method setProgress(float) on this view (used to talk to nested ConstraintLayouts etc.)
<CustomAttribute> call a set"name" method via reflection (limited to floats)

CustomAttribute

attributeName The name of the attribute. Case sensitive. ( MyAttr will look for method setMyAttr(...)
customFloatValue The value is a float looking setMyAttr(float )

KeyTrigger

motionTarget Id of the View or a regular expression to match layout_ConstraintTag
framePosition The point along the interpolation 0 = start 100 = end
onCross (method name) on crossing this position call this methods on the t arget
onPositiveCross (method name) on forward crossing of the framePosition call this methods on the target
onNegativeCross/td> (method name) backward crossing of the framePosition call this methods on the target
viewTransitionOnCross (ViewTransition Id) start a NoState view transition on crossing or hitting target
viewTransitionOnPositiveCross (ViewTransition Id) start a NoState view transition forward crossing of the framePosition or entering target
viewTransitionOnNegativeCross/td> (ViewTransition Id) start a NoState view transition backward crossing of the framePosition or leaving target
triggerSlack (float) do not call trigger again if the framePosition has not moved this fraction away from the trigger point
triggerId (id) call the TransitionListener with this trigger id
motion_postLayoutCollision Define motion pre or post layout. Post layout is more expensive but captures KeyAttributes or KeyCycle motions.
motion_triggerOnCollision (id) Trigger if the motionTarget collides with the other motionTarget

Standard attributes

android:visibility Android view attribute that
android:alpha Android view attribute that
android:elevation base z depth of the view.
android:rotation rotation of the view, in degrees.
android:rotationX rotation of the view around the x axis, in degrees.
android:rotationY rotation of the view around the y axis, in degrees.
android:scaleX scale of the view in the x direction.
android:scaleY scale of the view in the y direction.
android:translationX translation in x of the view.
android:translationY translation in y of the view.
android:translationZ translation in z of the view.

Summary

Nested types

protected interface MotionLayout.MotionTracker

Subclasses can override to build test frameworks

Listener for monitoring events about TransitionLayout.

Constants

static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int

Public fields

static boolean

Protected fields

boolean

Public constructors

MotionLayout(
    @NonNull Context context,
    @Nullable AttributeSet attrs,
    int defStyleAttr
)

Public methods

void

adds a listener to be notified of drawer events.

boolean
applyViewTransition(
    int viewTransitionId,
    MotionController motionController
)

Apply the view transitions keyFrames to the MotionController.

ConstraintSet

Creates a ConstraintSet based on an existing constraintSet.

void
enableTransition(int transitionID, boolean enable)

Disable the transition based on transitionID

void
enableViewTransition(int viewTransitionId, boolean enable)

Enable a ViewTransition ID.

void
fireTrigger(int triggerId, boolean positive, float progress)

This causes the callback onTransitionTrigger to be called

ConstraintSet

Get the ConstraintSet associated with an id This returns a link to the constraintSet But in most cases can be used. createConstraintSet makes a copy which is more expensive.

@IdRes int[]

Get the id's of all constraintSets used by MotionLayout

int

Return the current state id

ArrayList<MotionScene.Transition>

Get all Transitions known to the system.

DesignTool
int

Gets the state you are currently transition to.

int[]

Get the id's of all constraintSets with the matching types

float

Get current position during an animation.

MotionScene

Get the motion scene of the layout.

int

Gets the state you are currently transitioning from.

float

Gets the position you are animating to typically 0 or 1.

MotionScene.Transition
getTransition(int id)

This returns the internal Transition Structure

Bundle
long

Gets the time of the currently set animation.

float

Returns the last velocity used in the transition

void
getViewVelocity(
    View view,
    float posOnViewX,
    float posOnViewY,
    float[] returnVelocity,
    int type
)

Returns the last layout velocity used in the transition

boolean

Is initial state changes are applied during onAttachedToWindow or after.

boolean
boolean

Determines whether MotionLayout's touch &click handling are enabled.

boolean
isViewTransitionEnabled(int viewTransitionId)

Is transition id enabled or disabled

void
jumpToState(int id)

This jumps to a state It will be at that state after one repaint cycle If the current transition contains that state.

void
loadLayoutDescription(int motionScene)

This overrides ConstraintLayout and only accepts a MotionScene.

boolean

Intercepts the touch event to correctly handle touch region id handover

boolean
onNestedFling(
    @NonNull View target,
    float velocityX,
    float velocityY,
    boolean consumed
)
boolean
onNestedPreFling(@NonNull View target, float velocityX, float velocityY)
void
onNestedPreScroll(
    @NonNull View target,
    int dx,
    int dy,
    @NonNull int[] consumed,
    int type
)

React to a nested scroll in progress before the target view consumes a portion of the scroll.

void
onNestedScroll(
    @NonNull View target,
    int dxConsumed,
    int dyConsumed,
    int dxUnconsumed,
    int dyUnconsumed,
    int type
)

React to a nested scroll in progress.

void
onNestedScroll(
    @NonNull View target,
    int dxConsumed,
    int dyConsumed,
    int dxUnconsumed,
    int dyUnconsumed,
    int type,
    int[] consumed
)

React to a nested scroll in progress.

void
onNestedScrollAccepted(
    @NonNull View child,
    @NonNull View target,
    int axes,
    int type
)

React to the successful claiming of a nested scroll operation.

void
onRtlPropertiesChanged(int layoutDirection)
boolean
onStartNestedScroll(
    @NonNull View child,
    @NonNull View target,
    int axes,
    int type
)

React to a descendant view initiating a nestable scroll operation, claiming the nested scroll operation if appropriate.

void
onStopNestedScroll(@NonNull View target, int type)

React to a nested scroll operation ending.

boolean
void
void
void

This method is deprecated.

Please call rebuildScene() instead.

void

rebuild the motion Layouts

boolean

adds a listener to be notified of drawer events.

void
void
rotateTo(int id, int duration)

Rotate the layout based on the angle to a ConstraintSet

void

on completing the current transition, transition to this state.

void
setDebugMode(int debugMode)

Display the debugging information such as paths information

void

Initial state changes are applied during onAttachedToWindow unless this is set to true.

void
setInteractionEnabled(boolean enabled)

Enables (or disables) MotionLayout's onClick and onSwipe handling.

void

Set the transition position between 0 an 1

void
setOnHide(float progress)

Notify OnHide motion helpers

void
setOnShow(float progress)

Notify OnShow motion helpers

void
setProgress(float pos)

Set the transition position between 0 an 1

void
setProgress(float pos, float velocity)

Set the transition position between 0 an 1

void

Sets a motion scene to the layout.

void
setState(int id, int screenWidth, int screenHeight)

Set the State of the Constraint layout.

void
setTransition(int transitionId)

Set a transition explicitly to a Transition that has an ID The transition must have been named with android:id=...

void
setTransition(int beginId, int endId)

Set a transition explicitly between two constraint sets

void
setTransitionDuration(int milliseconds)

Change the current Transition duration.

void

Set a listener to be notified of drawer events.

void

Set the transition state as a bundle

String
void
touchAnimateTo(int touchUpMode, float position, float currentVelocity)
void
touchSpringTo(float position, float currentVelocity)

Allows you to use trigger spring motion touch behaviour.

void

Animate to the ending position of the current transition.

void

Animate to the ending position of the current transition.

void

Animate to the starting position of the current transition.

void

Animate to the starting position of the current transition.

void

Animate to the state defined by the id.

void
transitionToState(int id, int duration)

Animate to the state defined by the id.

void
transitionToState(int id, int screenWidth, int screenHeight)

Animate to the state defined by the id.

void
transitionToState(
    int id,
    int screenWidth,
    int screenHeight,
    int duration
)

Animate to the state defined by the id.

void

Not sure we want this

void
updateState(int stateId, ConstraintSet set)

update a ConstraintSet under the id.

void
updateStateAnimate(int stateId, ConstraintSet set, int duration)

Update a ConstraintSet but animate the change.

void
viewTransition(int viewTransitionId, View[] view)

Execute a ViewTransition.

Protected methods

void

Used to draw debugging graphics and to do post layout changes

void

This causes the callback TransitionCompleted to be called

long

Subclasses can override to define testClasses

MotionLayout.MotionTracker

Subclasses can override to build test frameworks

void
void
onLayout(boolean changed, int left, int top, int right, int bottom)
void
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void

block ConstraintLayout from handling layout description

void

Inherited Constants

From androidx.constraintlayout.widget.ConstraintLayout
static final int
static final String
VERSION = "ConstraintLayout-2.2.0-alpha04"
From android.view.View
static final int
static final int
static final int
static final int
static final int
static final int
static final Property<ViewFloat>
static final int
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE = "creditCardExpirationDate"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY = "creditCardExpirationDay"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH = "creditCardExpirationMonth"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR = "creditCardExpirationYear"
static final String
static final String
AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE = "creditCardSecurityCode"
static final String
static final String
static final String
static final String
static final String
static final String
static final String
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int
static final int
static final int
static final int
static final int
static final int
static final int[]
static final int[]
static final int[]
static final int[]
static final int
static final int
static final int
static final int
static final int
static final int
static final int
GONE = 8
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
KEEP_SCREEN_ON = 67108864
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
NO_ID = -1
static final int
static final int
static final int
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int[]
static final int[]
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final String
VIEW_LOG_TAG = "View"
static final int
static final int[]
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
From android.view.ViewGroup