Search code examples
androidandroid-motionlayout

How to change transition values according to orientation in a MotionLayout?


I create a scene that resizes a view from full to half the screen. It works on portrait mode, but the layout_height value is not enough for landscape mode. Is there a way to set a different value of a property in a XML scene file according to a specific orientation? If not, do I have to change it programmatically and how?

<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">
   <KeyFrameSet>
   </KeyFrameSet>
</Transition>

<ConstraintSet android:id="@+id/start">
    <Constraint
        android:id="@+id/content_parent"
        motion:layout_constraintEnd_toEndOf="parent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        motion:layout_constraintTop_toTopOf="parent"
        motion:layout_constraintStart_toStartOf="parent" />
</ConstraintSet>

<ConstraintSet android:id="@+id/end">
    <Constraint
        android:id="@+id/content_parent"
        motion:layout_constraintEnd_toEndOf="parent"
        android:layout_width="match_parent"
        android:layout_height="@dimen/section_height"
        motion:layout_constraintTop_toTopOf="parent"
        motion:layout_constraintStart_toStartOf="parent" />

</ConstraintSet>

UPDATE @ivan-wooll Here's how I did:

//dimens.xml
<dimen name="section_height" tools:ignore="ResourceCycle">@dimen/section_height</dimen>

//dimens.xml(port)
<dimen name="section_height">280dp</dimen>

//dimens.xml(land)
<dimen name="section_height">60dp</dimen>

Solution

  • I finally solved my problem with XML (@ivan-wooll suggestion, thumbs up) and Kotlin code (mine)

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        val csEnd = binding.motionLayoutVideo.getConstraintSet(R.id.end)
        var consHeight= resources.getDimensionPixelSize(R.dimen.section_height)
        csEnd.apply {
           constrainHeight(R.id.content_parent, consHeight)
        }
    }