1

I got a warning message that says:

[I18N] Hardcoded string "Something : " + someVariable, should use @string resource.

The line of code is: mTextView.setText("Something : " + someVariable);

I usually resolve this problem by extracting it to a string resource like :

mTextView.setText(mContext.getString(R.string.mSomething) + someVariable);

This obviously cannot be used when you have a variables which you want to display. So what is the correct way to display a String resource with a variable without getting a warning?

2 Answers 2

6

You can have arguments in your string resource. For your example, have:

<string name="mSomething">Something: %s</string>

%s is wildcard for a string argument.

Then, in your fragment / activity, do:

String text = getString(R.string.mSomething, someVariable.toString())

Then you can use this text further as needed. You can also call the getString() function on context if you are not in fragment / activity.

Sign up to request clarification or add additional context in comments.

2 Comments

The wildcard %s stands for the same in C as it stands in Android Studio? For example %d expects a number?
Sorry, I correct myself, there's wildcard for float - %f, %d is for non-decimal. They are quite the same as in C, that you are right.
2

Better solution is to use concat function instead '+' operator to avoid this warning message, so the code should be like:mTextView.setText(mContext.getString(R.string.mSomething).concat(someVariable);

1 Comment

Thanks, good to know, this works as well.

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.