• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Using placeholders with textView

Greum

Well-Known Member
Jun 2, 2019
150
50
Umbria, Italy
I'm trying to get mybrain around using placeholders with a textView. Instead of simply displaying "VAT" I want to show "VAT at 20%" (or whatever the rate is).

I currently have in my strings.xml

<string name="vat_text">VAT</string>

and in my Java

textView16.setText(R.string.vat_text);

I have changed the entry in strings.xml

<string name="vat_text">VAT at %d</string>

and tried in my Java

textView16.setText(getString(R.string.vat_text, vatPc));

but this gives a runtime error (which I couldn't catch as there were dozens of other errors afterwards.
 
  • Like
Reactions: Greum
Upvote 0
Thanks. I hadn't found that post on SO. I'll have a play with those.

It helps to pay attention to variable types!

<string name="vat_text">VAT at %1$.2f pc</string>

does the job, although I can't find how to include a % sign. It's not listed as one of the characters that needs to be escaped but bombs the script either way.

Edit. A bit more research and I discovered \%% did the job.
 
Last edited:
  • Like
Reactions: 23tony
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones