Search code examples
androidxmlkotlintextview

Easiest way to use integer number in TextView


I need confirmation of the most efficient way to show an integer number within a TextView. I've seen several dynamic ways of doing this but unsure of which to use. I also want the number to automatically change when a different writing script/device locale is used e.g. Arabic (Egpyt). In my example, I'm trying to display the number 2.

Kotlin (Option 1) - Did change but wondering if a better way exists

myTV.setText = getString(R.string.my_placeholder, 2)

Kotlin (Option 2) - Didn't change automatically

myTV.setText = 2.toString()

strings.xml

<string name="my_placeholder">%1$d</string>

Solution

  • Your 2nd option is more efficient, because getString() of 1st option needs to go to resources and find the right string for the locale and then do the pattern replacement etc.