Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 650
Farid Movsumov Avatar asked Feb 01 '12 20:02

Farid Movsumov


People also ask

What is the purpose of string xml in Android?

A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String. XML resource that provides a single string.

Why strings xml is useful?

xml , you can easily translate your whole app into other languages. This saves you a lot of work instead of doing this the hardcoded way: Android automatically selects the correct language based on user preferences, and you don't have to worry about selecting and displaying this language.

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

It allows you to easily locate text in your app and later have it translated. Strings can be internationalized easily, allowing your application to support multiple languages with a single application package file (APK).

In which folder can you find the string resource file strings xml?

The XML resource files containing localized strings are placed in subfolders of the project's res folder.


2 Answers

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.

like image 187
David Merriman Avatar answered Sep 28 '22 01:09

David Merriman


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

like image 27
paulsm4 Avatar answered Sep 27 '22 23:09

paulsm4