• 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
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.
 
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:
Back
Top Bottom