5

I have read in many places that you should declare your String objects in your resource file but I haven't read about any benefits anywhere.

I have already declared all my Strings that I have in my layouts as resources but I haven't done so in my classes.

My question is:

What are the benefits of declaring Strings as a resource? Are there any memory benefits?

1

3 Answers 3

6
  • Internationalisation,
  • Keeping all of your strings in a single place (where they can be editted globally),
  • Changing strings based on device (mdpi/large/portrait)... I mean, it'd be really rare for this last one, but it's possible.
  • Sharing the same string among many layouts (this will happen in any app which isn't tiny)
Sign up to request clarification or add additional context in comments.

Comments

2

The top one I reckon is: Translations! Put a new strings.xml in the right folder and the app translates itself for each device.

But there's a matter of organisation too. Just like the layout, you normally don't build in the code, because that's not the place for it. The code is to process stuff. The string is just one more of the resources that your code will use to show stuff on the screen.

Comments

1

One of the main benefits is for localization: you keep your code language-independent and just need to provide a different XML file for each language you want to support.

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.