0

in strings.xml I have one resource.

<string name="generic_price_with_rupee_symbol">\u20B9 %s</string>

This is what I do in Android data binding that works perfectly

<TextView
      ...
      android:text="@{@string/generic_price_with_rupee_symbol(item.price)}"
      />

Question:

How to use this resource in java code? As I don't want to make new resource.

I tried

textView.setText(getString(R.string.generic_price_with_rupee_symbol) + "100");

That gives wrong result and prints %s also.

3 Answers 3

2

The formatted values should be passed as parameter as second values to getString(int, Object..) method

where Object... is

The format arguments that will be used for substitution

so use

textView.setText(getString(R.string.generic_price_with_rupee_symbol, "100"));
//                                                                  ^^^
Sign up to request clarification or add additional context in comments.

Comments

1

It should be written like this -

textView.setText(getString(R.string.generic_price_with_rupee_symbol, "100"));

Check out String getString (int resId, Object... formatArgs) from Documentation.

Comments

1

Use the following:-

textView.setText(getResources().getString(R.string.generic_price_with_rupee_symbol, "100"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.