Anyone can help me with the correct way to change TextView
's text in MotionLayout
... this is what I doing.
I'm testing the MotionLayout
on a Simple App...
I reach the part on the Motion tutorials about CustomAttributes
With them you can change the BackgroundColor
of a View
, also the textColor
using customColorValue
In this case you can see it works very well changing this values in the start and end scene:
<CustomAttribute
motion:attributeName="backgroundColor"
motion:customColorValue="#004A6D" />
<CustomAttribute
motion:attributeName="textColor"
motion:customColorValue="#000000" />
Also I note that there is a customStringValue
so I think I can change the TextView
text to "BEFORE" -> "AFTER". But when I try to set this with CustomAttribute
the app crash.
In the start scene:
<CustomAttribute
motion:attributeName="Text"
motion:customStringValue="BEFORE" />
And in the final scene:
<CustomAttribute
motion:attributeName="Text"
motion:customStringValue="AFTER" />
Outside MotionScene the textView
text is TEST:
CustomAttribute
only for the end scene... the text change from initial value TEST to the end AFTER value... so it partially works but never return to the initial state.TextView
. It works partially.So... anyone can help me with the correct way to change TextView
's text in MotionLayout
.
MotionLayout is a layout type that helps you manage motion and widget animation in your app. MotionLayout is a subclass of ConstraintLayout and builds upon its rich layout capabilities. As part of the ConstraintLayout library, MotionLayout is available as a support library and is backwards-compatible to API level 14. Figure 1.
You will be using a 3rd party library called FadingTextView to help you make this smooth TextView animation. 1- Open up Android Studio and open any project that you have in your computer. Create new Android Studio project or open existing project. ( Large preview) 2- Open up build.gradle (module:app) and add the library in the dependencies.
I'm testing the MotionLayout on a Simple App... I reach the part on the Motion tutorials about CustomAttributes With them you can change the BackgroundColor of a View, also the textColor using customColorValue In this case you can see it works very well changing this values in the start and end scene:
Android – EditText on text change EditText is used to read input from user. A listener could be attached to the EditText to execute an action whenever the text is changed in the EditText View.
you can do it programmatically using a TransitionListener like this:
motionLayout.setTransitionListener(object : MotionLayout.TransitionListener {
override fun onTransitionTrigger(p0: MotionLayout?, p1: Int, p2: Boolean, p3: Float) {
// ADD YOUR CODE HERE
}
override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) {
// ADD YOUR CODE HERE
}
override fun onTransitionChange(p0: MotionLayout?, p1: Int, p2: Int, p3: Float) {
textView.text = if(p3==0f) "before" else "after"
}
override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {
}
})
Easy way of doing this is
<ConstraintSet android:id="@+id/start">
<Constraint
android:id="@id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<CustomAttribute
app:attributeName="textColor"
app:customColorValue="#1D1D1D" />
</Constraint>
</ConstraintSet>
<Constraint
android:id="@id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<CustomAttribute
app:attributeName="textColor"
app:customColorValue="#FFFFFF" />
</Constraint>
</ConstraintSet>
Change motion to app
I test and like this
//constraintSetStart
<ConstraintSet...>
<Constraint
android:id="@+id/button">
<CustomAttribute
motion:attributeName="Text"
motion:customStringValue="CLOSE"/>
</Constraint>
</ConstraintSet>
//constraintSetEnd
<ConstrainSet..>
<Constraint
android:id="@+id/button">
<CustomAttribute
motion:attributeName="Text"
motion:customStringValue="OPEN"/>
</Constraint>
</ConstraintSet>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With