I want to add animation in android text view so that when a text changes it should change smoothly and slowly. Like, fade in or fade out when the text changes. Is it possibl
This is my extension for fadOut fadIn a new text.
// TextViewExtensions
fun TextView.setTextAnimation(text: String, duration: Long = 300, completion: (() -> Unit)? = null) {
fadOutAnimation(duration) {
this.text = text
fadInAnimation(duration) {
completion?.let {
it()
}
}
}
}
// ViewExtensions
fun View.fadOutAnimation(duration: Long = 300, visibility: Int = View.INVISIBLE, completion: (() -> Unit)? = null) {
animate()
.alpha(0f)
.setDuration(duration)
.withEndAction {
this.visibility = visibility
completion?.let {
it()
}
}
}
fun View.fadInAnimation(duration: Long = 300, completion: (() -> Unit)? = null) {
alpha = 0f
visibility = View.VISIBLE
animate()
.alpha(1f)
.setDuration(duration)
.withEndAction {
completion?.let {
it()
}
}
}
textView. setTextWithAnimation("Hello world", 500)