I'm just starting to play with MotionLayout
. I have defined an activity layout using MotionLayout
that uses a MotionScene
to hide and show a view.
The MotionScene
transition looks like this:
<Transition
app:constraintSetStart="@id/collapsed"
app:constraintSetEnd="@id/expanded">
<OnClick app:target="@id/nextButton" />
</Transition>
Troublie is, nothing happens when I programmatically add a ClickListener to the button:
nextButton.setOnClickListener {
//do some stuff
}
This listener is completely ignored but the transition (view expanding/collapsing) is triggered with every click. I have seen where someone extends MotionLayout
to handle click events but it seems like there might be an easier way to add another click listener for the button.
Question 1: Is there a way to add a ClickListener to the target of an OnClick in a MotionLayout transition?
Question 2: Is there a way to make the transition a one time only event? The desired result is that if the view is collapsed when the button is clicked, then the view expands, but if it's already expanded then it stays expanded.
Lastly, I'm using the namespace "http://schemas.android.com/apk/res-auto"
and the docs clearly state that target
and mode
are attributes for OnClick. But the project won't compile when I use mode
because it can't be found in that namespace.
Question 3: Am I using the correct namespace?
<OnClick
motion:targetId="@+id/rateUsButton"
motion:clickAction="transitionToEnd"/>
Just ran into the same issue today. I was able to intercept the click by using setOnTouchListener
instead of setOnClickListener
in my code.
rateUsButton.setOnTouchListener { _, event ->
if (event.action == MotionEvent.ACTION_UP) {
// handle the click
}
false
}
I know this solution isn't the best but I didn't find another option. Returning false means the touch was not handled here and thus will be handled by the motion layout.