Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefit of Declaring String as Resource in strings.xml file

Tags:

android

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?

like image 795
StuStirling Avatar asked Nov 09 '12 15:11

StuStirling


People also ask

What is the advantage of making strings in strings XML file?

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.

Why do we store strings as resources in xml?

The purpose of strings. xml (and other *. xml resource files) is to regroup similar values in one place. This facilitates finding values that would be otherwise buried in the code.

Why should you use string resources instead of hard coded strings?

It is not good practice to hard code strings into your layout files. You should add them to a string resource file and then reference them from your layout. This allows you to update every occurrence of the word "Yellow" in all layouts at the same time by just editing your strings.

What is the use of strings xml?

String. xml file contains all the strings which will be used frequently in Android project. String. xml file present in the values folder which is sub folder of res folder in project structure.In Android Studio, we have many Views such as TextView,Button,EditText,CheckBox,RadioButton etc.


3 Answers

  • 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)
like image 55
Graeme Avatar answered Oct 01 '22 11:10

Graeme


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.

like image 20
Budius Avatar answered Oct 01 '22 13:10

Budius


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.

like image 36
sdabet Avatar answered Oct 01 '22 12:10

sdabet