Search code examples
androidandroid-motionlayout

How to set the state end in Java code MotionLayout


I use MotionLayout

I want to manage the @+id/end of the ConstraintSet in Java

for example

<ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@+id/item_c"
            motion:layout_constraintWidth_percent="0.5"
            motion:layout_constraintEnd_toEndOf="parent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            motion:layout_constraintHeight_percent="0.3"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintHorizontal_bias="1"
            motion:layout_constraintVertical_bias="1.0" />
    </ConstraintSet>

I want to set the "layout_constraintHorizontal_bias" value in Java on the switch

But I did not find a solution

what should I do?


Solution

  • ConstraintSet should have all the methods you need

    MotionLayout ml = ...
    ConstraintSet set = ml.getConstraintSet(R.id.end)
    set.setHorizontalBias(R.id.widgetid, 0.4)
    ml.updateState(R.id.end, set);
    ml.rebuildScene();
    

    If you are already in that state you might want to call

    ml.updateStateAnimate(R.id.end,set,duration_in_ms);