Search code examples
androidstringkotlinsettext

Android Kotlin Do not concatenate test displayed with setText. Use resource string with placeholders


And also "String literal in setText cannot be translated "

Now there are posts on this, but nothing seems working for me. Or need a proper explanation. How to use string resources Getting warning on this

 pTxt.text = "Total : $ $price"

Here, price is a value

if use this

pTxt.setText(R.string.displayPriceMsg, price)

it gives an error.

tried String.format() but giving garbage value.

Have this in strings.xml

<string name="displayPriceMsg">Total : $ %1$d</string>

Solution

  • You have to pass format arguments to the getString method:

    pTxt.text = context.getString(R.string.displayPriceMsg, price)