I have recently tried the MotionLayout
, I works fine on a button when it is a direct child of the MotionLayout
but the same motion scene does not work, when I enclose the button in another layout, bu the parent layout is still MotionLayout
.
First layout where the button is direct child :-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
app:layoutDescription="@xml/demo"
android:layout_height="match_parent"
tools:context=".Demo" >
<Button
android:layout_width="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_height="wrap_content"
android:id="@+id/yellow_button"
/>
</androidx.constraintlayout.motion.widget.MotionLayout>
Second layout where button is indirect child :-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
app:layoutDescription="@xml/demo"
android:layout_height="match_parent"
tools:context=".Demo"
>
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/l1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/yellow_button"/>
</LinearLayout>
</androidx.constraintlayout.motion.widget.MotionLayout>
The motion scene layout is below:-
<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ConstraintSet android:id="@+id/start">
<Constraint android:id="@+id/yellow_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
<CustomAttribute app:attributeName="alpha"
app:customFloatValue="0.0"/>
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<Constraint android:id="@id/yellow_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="1.0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
<CustomAttribute app:attributeName="alpha"
app:customFloatValue="1.0"/>
</Constraint>
</ConstraintSet>
<Transition
app:constraintSetEnd="@id/end"
app:autoTransition="animateToEnd"
app:constraintSetStart="@+id/start"
app:duration="2000"/>
Is there any guideline that needs to be followed in these cases??
OR
Does this mean that only direct children of the MotionLayout
can be animated with it?
This medium article (https://medium.com/google-developers/introduction-to-motionlayout-part-i-29208674b10d) by Google Developers says under the 'Limitations' section: "MotionLayout will only provide its capabilities for its direct children — contrary to TransitionManager, which can work with nested layout hierarchies as well as Activity transitions."