This question already has answers here:
Why to use strings.xml?
(3 answers)
Closed 7 years ago.
Commonly android programmer use string.xml because it recommended by ADT android like android studio or eclipse (at least that what I thought) , but lately I think using string.xml is waste of resources ... we can directly named the text on layout widget or app name directly instead of declare it on string.xml and call it on layout later ...
Can any one explain that my thought is right or wrong ?
Thank you
It's possible to directly name your text. But resources are really helpful when your text have to change depending of your users' configurations, such as language. That's why it is recommanded to do it this way.
It is also a way to avoid having too many informations to manage in one file for the developper so he can focus on the way he wants his layout to look like. Android IDEs make string resources easy to use for you. I would recommand you keep using them.
There is a reason you use strings.xml and that is to support multiple locale just to provide one point. Also its easily extensible as compared to hardcoding your strings...
One of its use that You can use multiple languages for your app using string.xml
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>
It will automatically pick up your default language selected.
and other is, suppose you are using same string multiple places. so in XML, it would be easy to change by changing in only one place.
If you use string.xml you will have a global access to the all string variables used in the application .
If you hard-code the string values in the xml resources , you will have slight improvement in UI rendering
Related
So, i kind of created a monster. Built an android game for few years and used lots of hardcoded strings. Now i want to support different languages. My questions - is there an automatic way to create these resources?
for example tell Android studio for all the "hard coded" warnings apply extract string resource.
I aw aware of the inspect code that show you localization warnings, i am looking for a way to automate the fix
AFAIK, it's not possible to fix that automatically. You need to press Alt+Ctrl+Shift+i and type hardcoded strings. After completion of the finding process, manually create them in strings.xml.
I am attaching a link to site that will convert your string.xml to different language you want and then download translated string.xml in your stings folder, it will translate each string step by step, if this is what you want, hope it helps
https://asrt.gluege.boerde.de
I have an Android project that I developed one year ago. I didn't think in do the project multi-language and now I need support it.
There are any easy way to detect all strings and generate the XML file? Or I need modify the project string for string?
The project is developed in Eclipse.
Android provides a very simple way to localize apps: string resources.
You need to provide several strings.xml files.
Each in a directory called /res/values-xy, where xy is the language (i.e.: es, fr, en, de, it, ...).
Then just refer these strings in your project, like R.string.my_string_name (in Java) or #string/my_string_name (in xml)
For reference: http://developer.android.com/training/basics/supporting-devices/languages.html
[EDIT]
Same goes for arrays: just use /res/values-xy/arrays.xml
Note: the names strings.xml and arrays.xml are just conventional ones can be changed to anything you like better.
AFAIK You need to modify the strings in your project manually. If you have hard-coded strings in your layouts then you can use Lint to find out all the hard-coded strings. Put them then into values/strings and the translation should go to the respective folder of each language.
EDIT:
If you're running Eclipse you can use the search feature to help you track all your hard-coded String. Check this topic
I am a bit noob in Android and recently I found out that I can use the predefined string that Android provides as #android:string/cancel or #android:string/ok. At first I thought it was a good idea to use them because is a way to reuse code, but now I am not so sure about that.
What if somebody with a device configured with a language that I don't support install my app?
I assume that the app will use a default language, probably english, but those string from #android:string will get translated to the user's language, so he will end up with a mix of languages.
It this true? What do you think about use #android:string?
Thanks!
EDIT: Apparently my question hasn't been understood properly. I am NOT asking about how to support different languages. My question is about the convenience of use strings defined on #string:android, if it is correct to use them or can be lead to undesirable situation like a mix up of languages in the same application.
To ensure that your strings are appearing properly on devices configured with different languages, you'll want to create different values directories for different languages. For example, your default string values would be kept under values/strings.xml and French string values would be kept under values-fr/strings.xml.
The Android Developer website gives you plenty of information for supporting different languages in your application. http://developer.android.com/training/basics/supporting-devices/languages.html
The android: values (strings, icons, themes, etc.) will differ between devices and Android versions. If you want to use them, it's safest to copy them into your project. So for strings, you wouldn't have to worry about partial translation.
In the ressource folder of your app (res), ther is a folder "values" in it, and in this folder is the string ressource xml (strings.xml).
Usually, your app selects the strings from this file.
But you can add other value folders like this: Just create a new folder and name it "values-countryCode", for example "values-ch" for Switzerland ;)
Your app automaticly chooses the right string ressource, depending on your device's langague settings. If the langague of your device isn't available, it just takes the sting ressource of the default "values" folder.
A list if the country-codes is here.
Further information can be found here.
Hope I helped, and this is what you're looking for!
This question already has answers here:
Why to use strings.xml?
(3 answers)
Closed 8 years ago.
In creation of UI in android projects ,why we have to refer values of string from the values folder's string.xml?
why eclipse shows warning if we assign values straight away?
I want to know the logic behind this,It's a very basic question but i couldn't find answer for this from google!
Another reason that is not covered in the older post pointed out by KenWolf, is that now when uploading your application to the Google Play Store, you can easily make your app more accessible to other languages/locations by uploading just your strings.xml to their new translation service announced at I/O 2013. This just has to do with localizing your resources like was stated here.
You can read more about it here.
There are many different advantages to using a string resource file, rather than hardcoding strings. While it may seem pointless and convoluted in small projects, it is incredibly useful in larger projects.
If you know that you want the same string to appear in multiple places, possible occurring in separate activities, the string resource file allows you to reference one source, rather than type the string over and over again.
If you decide to change a string in you app, or have a dynamically changing string, it is much easier to modify the resource file than it is to manually change all instances of that string in you app
It is great for localization and translation. If someone wants to port your app to a different language, all you have to do is substitute the strings.xml with a translated version
Thus, it may seem useless in small applications, but for large scale applications, it makes your life much simpler and allows you to more easily reach foreign language users.
The reason we are referring from String.xml is we can reuse the same string where ever we want.
Someone please explain what is the main idea of using strings.xml?
I think it would be useful for multi-language support but how can we organise it for that?
Do I need it if I don't want to use multi-language support in my android application?
The idea is that it represents a single location for various strings, so your code isn't peppered with string literals. In addition to that, you gain the ability to easily localize. Organization of files for localization is covered here:
http://developer.android.com/guide/topics/resources/localization.html#creating-alternatives
Do you need it if you're not localizing? No. But it may make things easier in the long run, and I would recommend using it just for that reason.
Hard-coding strings is Bad.
Parameterizing strings (e.g. with strings.xml) is Good.
Being able to internationalize your strings (with language and/or locale-specific versions of strings.xml) is even Better :)
PS:
To make use of internationalization, just create resource subdirectories. Google will give you plenty of references/examples. Herre's one:
http://developer.android.com/guide/topics/resources/localization.html
* res/values/strings.xml
Contains English text for all the strings that the application
uses, including text for a string named title.
* res/values-fr/strings.xml
Contain French text for all the strings, including title.
* res/values-ja/strings.xml
Contain Japanese text for all the strings...
And yes, you should absolutely get in the habit of using strings.xml (and colors.xml and dimens.xml etc etc) even if you don't plan on internationalizing immediately.
IMHO....