0

I am trying to do something like this:

mMovieName.setText(R.string.name + movieDetailsResponse.getTitle());

Expected Display : Name: Titanic

Actual Display : 2131165233Titanic

Can someone tell me how can I achieve expected display. I dont want to hardcode the string in TextView like this :

mMovieName.setText("Name: " + movieDetailsResponse.getTitle());

Thanks for any help

4 Answers 4

7

You can use Formatting strings.

Your strings.xml:

<string name="name">Name: %1$s</string>

Your Java code:

mMovieName.setText(getResources().getString(R.string.name,  movieDetailsResponse.getTitle()));

Output:

Name: Titanic

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

Comments

1

Hello use below code,

mMovieName.setText(getString(R.string.name) + movieDetailsResponse.getTitle());

6 Comments

Thanks .. this worked.. I will accept the answer in 10 minutes as the system wont allow me to it before that
@Sushil maybe it worked but this is not a good solution, see Niranj Patel answer instead (using Resources#getString(int id, Object... formatArgs))
@pskink .. both are same solution. As I was using the function in fragment, I didnt need to explicitly call getResources
@Sushil no they are not, if you need a fragment then simply use Fragment#getString(int resId, Object... formatArgs)
@Sushil It's not the same. One uses formatting strings (good), one uses concatenation, which is no better than hardcoding (bad).
|
0

You need to replace R.string.name with getResources().getString(R.string.name) because R.string.name is not a string but an Id of this string.

mMovieName.setText(getResources().getString(R.string.name) + movieDetailsResponse.getTitle());

Comments

-1

You should do

mMovieName.setText(getResources().getString(R.string.name) + movieDetailsResponse.getTitle());

5 Comments

You should not. What if in another language the movie title goes first?
@EugenPechanec he asked why the string was being returned as an int and I answered his question. Also he never asked or said that he might want to use another language. And finally, that is not relevant since in the example that I said Name: will always appear first.
he asked [...] and I answered his question. I must disagree, you posted some code without an explanation. You also ignored the second part of the question I dont want to hardcode the string. Concatenating two strings in Java is still a form of hardcoding (the order of strings is defined at use site). Formatting strings (the accepted answer) is better option. /// I like to think the community is here to help askers grow, in this case help OP become better Android developer by learning best practices.
Your answer is also redundant as two other people came up with the exact same thing. Always provide extra value.
Your answer is also redundant as two other people came up with the exact same thing. When I answered the question, there was no other answer at that time. I agree that the answered selected is better but I don't agree that concatenating two strings is a form of hardcoding. For me that is a little farfetched.

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.