Search code examples
androidandroid-motionlayout

Android: MotionLayout not allowing child to expand and fill the screen


I'm using a fragment in a MotionLayout but the fragment view isn't filling the MotionLayout even though all constraints are set. Not only fragment, even any view, such as, LinearLayout doesn't expand and I get this :

enter image description here

If I change MotionLayout to ConstraintLayout it works fine and the fragment fills the parent.

Here is my layout xml code:

    <?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"> 

        <androidx.constraintlayout.motion.widget.MotionLayout
            android:id="@+id/motion_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
            app:layoutDescription="@xml/scene_drawer"
            tools:context=".activity.MainActivity">  
            <fragment
                android:id="@+id/nav_host"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintLeft_toLeftOf="left" 
                app:defaultNavHost="true"
                app:navGraph="@navigation/main" 
                />
        </androidx.constraintlayout.motion.widget.MotionLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/navView"
            android:layout_width="120dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/navdrawer_menu"
            app:headerLayout="@layout/nav_header"
            />
    </androidx.drawerlayout.widget.DrawerLayout>
</layout>

Solution

  • I found the problem to be that in my motion scene xml, I needed to make the start and end constraint sets' width and height as match parent.

        <?xml version="1.0" encoding="utf-8"?>
    <MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:motion="http://schemas.android.com/apk/res-auto">
    
        <Transition
            motion:constraintSetEnd="@+id/end"
            motion:constraintSetStart="@+id/start"
            motion:duration="500"
            motion:motionInterpolator="linear"
            />
    
        <ConstraintSet android:id="@+id/start">
            <Constraint
                android:id="@id/nav_host"
                android:layout_width="match_parent"  
                android:layout_height="match_parent"  
                motion:layout_constraintTop_toTopOf="parent"
                motion:layout_constraintWidth_default="percent"
                motion:layout_constraintWidth_percent="1"
                />
        </ConstraintSet>
    
        <ConstraintSet android:id="@+id/end">
            <Constraint
                android:id="@id/nav_host"
                android:layout_width="match_parent"  
                android:layout_height="match_parent"  
                android:layout_marginLeft="100dp"
                android:scaleX="0.8"
                android:scaleY="0.8"
                android:translationX="100dp"
                motion:layout_constraintWidth_default="percent"
                motion:layout_constraintWidth_percent="1"
                />
        </ConstraintSet>
    </MotionScene>