Qazi Fahim Farhan
Qazi Fahim Farhan

Reputation: 2176

Is it possible to switch from Start state to End state in android MotionLayout using Java/Kotlin?

I am working on an android project that uses MotionLayout. Long story short, I want to programmatically switch between the Start and End states using Java. Say, this should happen on clicking a button. So I am looking for something like this:

MotionLayout myMotionLayout = findViewById(R.id.myMotionLayout);
Button button = findViewById(R.id.button);

button.setOnClickListener( v -> {
  // some code that switches the state
}
);

I did a lot of Google but could not find something like that. I came across a method of the MotionLayout named setTransitionListener, but it is used for doing things during the change, not to trigger the change.

I don't know what to do. Can anyone help me with this? Thank you.

Upvotes: 1

Views: 1546

Answers (1)

Natig Babayev
Natig Babayev

Reputation: 3296

You can use myMotionLayout.transitionToEnd() and myMotionLayout.transitionToStart():

MotionLayout myMotionLayout = findViewById(R.id.myMotionLayout);
Button button = findViewById(R.id.button);

button.setOnClickListener( v -> {
    if(myMotionLayout.getCurrentState() == R.id.start_state) {
        myMotionLayout.transitionToEnd()
    } else {
        myMotionLayout.transitionToStart()
    }
  }
);

Upvotes: 1

Related Questions