Search code examples
androidformattingandroid-resources

Android string format with ValueAnimator


I want to show a price by using String resource with ValueAnimator.
The output what i expect is like this : (123,432,133)

strings_format.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="price_format">%,s</string>
    
</resources>

ValueAnimator

private fun setAnimatedTextView(
initialValue: Int = 0,
finalValue: Int = 0,
textView: TextView
) {
  ValueAnimator.ofInt(initialValue, finalValue).apply {
      duration = 1_000
      addUpdateListener {
          textView.text = requireContext().getString(R.string.price_format, it.animatedValue.toString())
      }
  }.start()
}

But if i run this code, it makes FormatFlagsConversionMismatchException.
How can i make it?


Solution

  • I did a mistake.
    First of all, i changed the strings_format.xml like this.

    <resources>
    
        <string name="price_format">%,d</string>
        
    </resources>
    

    and the part of using ValueAnimator is like this.

        private fun setAnimatedTextView(
        initialValue: Int = 0,
        finalValue: Int = 0,
        textView: TextView
    ) {
      ValueAnimator.ofInt(initialValue, finalValue).apply {
          duration = 1_000
          addUpdateListener {
              textView.text = String.format(requireContext().getString(R.string.price_format, it.animatedValue.toString().toInt()))
          }
      }.start()
    }