4

I´ve got the problem, that Android Studio reminds me to use the string.xml for set the text. The following gives me the hint: Do not concatenate text displayed with setText. Use resource string with placeholders.

public int points = 0;
public TextView tv_points;
tv_points.setText("Points: " + points);

Okay if i use the string xml with this code it does not what i want:

<string name="points_string">Points: </string>    

public int points = 0;
public TextView tv_points;
tv_points.setText((R.string.points_string) + points);

It brings no error and no hint, but is wrong. I do not get the wanted effect to set the points.

3
  • 2
    use this tv_points.setText(getString(R.string.points_string).concat(String.valueOf(points))); Commented Feb 2, 2018 at 18:59
  • String.format(""); is also valid to use for such cases Commented Feb 2, 2018 at 18:59
  • The reason you don't concatenate is that "points: 9" may not be the right way to say it in every language. You want translaters to be able to alter it as needed, which may include moving elements around. This is more applicable for text than labels, but still makes sense here. Commented Feb 2, 2018 at 19:06

1 Answer 1

7

You need to format the string using strings.xml like this:

<string name="points_string">Points: %1$d</string>

Then you can use it like this:

tv_points.setText(getResources().getString(R.string.points_string, points));

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

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.