Reputation: 9079
Is there really a need to specify strings in an XML file. Android always shows warning when strings are directly written and not extracted from strings.xml. Is there any advantage by using XML
Upvotes: 2
Views: 1049
Reputation: 82938
The most important use is to handle localization.
At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.
For example, the following are some different string resource files for different languages.
<string name="title">My Application</string> <string name="hello_world">Hello World!</string>
<resources> <string name="title">Mi Aplicación</string> <string name="hello_world">Hola Mundo!</string> </resources>
<resources> <string name="title">Mon Application</string> <string name="hello_world">Bonjour le monde !</string> </resources>
In your source code, you can refer to a string resource with the syntax R.string.. There are a variety of methods that accept a string resource this way.
// Get a string resource from your app's Resources
String hello = getResources().getString(R.string.hello_world);
// Or supply a string resource to a method that requires a string
TextView textView = new TextView(this);
textView.setText(R.string.hello_world);
From Supporting Different Languages
Read Localizing with Resources for details, and Android Localization Tutorial is a very good tutorial about the same.
Upvotes: 5
Reputation: 38409
We added string on strings.xml, because we can easily translate our whole app into other languages.
So in the folder values you would have strings.xml with this content:
<string name="hello">Hello</string>
In values-fr a strings.xml with this content:
<string name="hello">Bonjour</string>
If you want to make your application support different - 2 language than you have to follow string.xml.
Upvotes: 4