ANDROID: What is the main idea behind of using strings.xml? - android

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....

Related

Converting hard coded strings to string resources

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

Different initial string files

is there any way to use different string files? I know that I can create files for another languages, but I want to load different files for English, depending on some initial value. It will be good if I could have one more file with default value. It is possible?
Android string resources are optimized for supporting different locales and you can force app to use particular locale. If you want use custom dictionaries for one language I would recommend to use custom class that will read string-array resources. Imho nice example is: Dirty Phrasebook
Few articles about this app,
Source code

Which is Better use or don't use string.xml? [duplicate]

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

Using strings from #android:string

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!

Defining Strings in res/values/string.xml?

What's the point of defining strings in xml at the res/values/string.xml directory? Is it more efficient than just defining the strings as constants within your classes? (e.g. Database table creation scripts, etc..)
Is it a matter of organization or is there some benefit in how Android handles these objects in memory?
So you can easily translate them into different languages.
So they're nicely organized and you have them all in one place.
You can easily translate your app if you use strings.xml. Just create a new folder with suffix like values-cs and put the xml with czech strings in it and the whole app will be translated to czech if you have set czech localization in your phone.
applications access them/compute them faster than normal strings
localization
The hello string is defined in the res/values/strings.xml file. This is the recommended practice for inserting strings to your application, because it makes the localization of your application to other languages graceful, without need to hard-code changes to the layout file.
language (computer) translation
For me the first point is the deal breaker, anything to make your app faster. (this is assumed from the countless hours of creating my own applications and being told using string.xml is best from an optimization point of view, plus, especially in a long listview, it does seem to load faster for me (droid A855) )

Categories

Resources