How to apply color transition for textview from left to right android?
You should write this with ValueAnimator
Kotlin code:
fun changeTextColor(textView: TextView, fromColor: Int, toColor: Int, direction: Int = View.LAYOUT_DIRECTION_LTR,duration:Long = 200) {
var startValue = 0
var endValue = 0
if(direction == View.LAYOUT_DIRECTION_LTR){
startValue = 0
endValue = textView.text.length
}
else if(direction == View.LAYOUT_DIRECTION_RTL) {
startValue = textView.text.length
endValue = 0
}
textView.setTextColor(fromColor)
val valueAnimator = ValueAnimator.ofInt(startValue, endValue)
valueAnimator.addUpdateListener { animator ->
val spannableString = SpannableString(
textView.text
)
if (direction == View.LAYOUT_DIRECTION_LTR) spannableString.setSpan(
ForegroundColorSpan(
toColor
), startValue, animator.animatedValue.toString().toInt(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
else if (direction == View.LAYOUT_DIRECTION_RTL) spannableString.setSpan(
ForegroundColorSpan(
toColor
), animator.animatedValue.toString().toInt(),spannableString.length , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
textView.text = spannableString
}
valueAnimator.duration = duration
valueAnimator.start()
}
And simply use:
changeTextColor(
yourTextView,
Color.BLACK,
Color.WHITE,
View.LAYOUT_DIRECTION_LTR,
300
)