How to run particular transition in MotionLayout?
I am not sure what you are doing (not enough code) but here is a as simple as I can make it 5 state example.
<MotionScene
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:constraintSetStart="@id/state1"
motion:constraintSetEnd="@+id/state2"/>
<Transition
motion:constraintSetStart="@id/state2"
motion:constraintSetEnd="@+id/state3"/>
<Transition
motion:constraintSetStart="@id/state3"
motion:constraintSetEnd="@+id/state4"/>
<Transition
motion:constraintSetStart="@id/state4"
motion:constraintSetEnd="@+id/state5"/>
<Transition
motion:constraintSetStart="@id/state1"
motion:constraintSetEnd="@+id/state5"/>
<ConstraintSet android:id="@+id/state1">
<Constraint android:id="@+id/view">
<CustomAttribute motion:attributeName="text" motion:customStringValue="state1" />
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/state2">
<Constraint android:id="@+id/view">
<Transform android:translationX="-100dp"/>
<CustomAttribute motion:attributeName="text" motion:customStringValue="state2" />
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/state3">
<Constraint android:id="@+id/view">
<Transform android:translationX="100dp"/>
<CustomAttribute motion:attributeName="text" motion:customStringValue="state3" />
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/state4">
<Constraint android:id="@+id/view">
<Transform android:translationY="-100dp"/>
<CustomAttribute motion:attributeName="text" motion:customStringValue="state4" />
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/state5">
<Constraint android:id="@+id/view">
<Transform android:translationY="100dp"/>
<CustomAttribute motion:attributeName="text" motion:customStringValue="state5" />
</Constraint>
</ConstraintSet>
this scene file works with this main_activity.xml
<?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"
android:layout_height="match_parent"
app:layoutDescription="@xml/activity_main_scene"
android:id="@+id/motionlayout"
tools:context=".MainActivity">
<TextView
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#4DD3D3"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="transition"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:onClick="nextState"
/>
</androidx.constraintlayout.motion.widget.MotionLayout>
and this MainActivity.kt
package com.example.multistate
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun nextState(view: View) {
when (motionlayout.currentState ) {
R.id.state1 -> motionlayout.transitionToState(R.id.state2)
R.id.state2 -> motionlayout.transitionToState(R.id.state3)
R.id.state3 -> motionlayout.transitionToState(R.id.state4)
R.id.state4 -> motionlayout.transitionToState(R.id.state5)
R.id.state5 -> motionlayout.transitionToState(R.id.state1)
}
}
}
Unless you have it should only transition to the state you call if you call ml.transitionToState(R.id.xx);
Remove all autoTransition attributes in you motionScene