I've a question about how i can handle 2 different language string resources like .e.g
#string/buttontext_de and #string/buttontext_en related to ONE menu button?
I've one start button and i want to display the right string resource which user device language is set.
1) I've 2 directories, one "res\values" (its the default german values directory) and one "res\values-en" where i've put in the english text resource (in strings.xml)
2) Next step i've to set the localization code in my start activity?
3) How can i set code that my button text is choosing the right language string resource?
THX a lot for any help!
See the Localization Developer Guide. Just specify duplicate String resources in the appropriate folders, step back, and let the I18N magic happen. To test it, go into your Settings (Einstellungen) and change your system language preference.
Thanks to Bytebender for pointing out what I missed. Yes, you want to create these files and resources:
Inside res/values-en/strings.xml:
<string name="buttontext">Click Me</string>
Inside res/values-de/strings.xml:
<string name="buttontext">Hier Klicken</string>
Then refer to it in your XML like this:
<Button
android:id="#+id/myBtnId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doHandleBtnClick"
android:text="#string/buttontext" />
Related
As soon as I drag/drop a button from Palette into the XML file, Android Studio gives me the following error when I click on "Show Warnings and Errors":
Message: [I18N] Hardcoded string "Button", should use `#string` resource Suggested Fixes:
- Extract string resource
- Suppress: Add tools:ignore="HardcodedText" attribute
Inside the Button code in Text the following Android Studios enters itself automatically when I dragged/dropped button into XML file:
android:text="Button"
Do I just always have to click "Suppress" choice because I don't know what they want me to change for "Extract String Resource" choice. Or is it asking me to change Android Studio's code that it sets itself automatically?
You can do this instead of suppressing:
Add a String resource in res/values/strings.xml:
<string name="button_label">Button</string>
Set the above resource as text for your Button:
android:text="#string/button_label"
It is not an Error, this is just Warning that say you should use string resources, but not hardcoded strings in your code. But you can use hadrdcoded Strings and all will work fine.
Using String resources simplifies your life. For example when you want to localize your app. See screenshot.
using string resources
You can set string res. to TextView.Text, then create few android resource directories with names values, values-ru, values-uk etc.
In this direcroties you should create file strings.xml for each.
and Override your string like
<string name="message"> Message </string> in values
<string name="message"> Сообщение </string> in values-ru etc.
Now if you change your device language, from en to ru , in your app in textView will be written "Сообщение"
It is also useful in several other cases.
Message: [I18N] Hardcoded string "Button", should use #string
resource
Suggested Fixes:
Extract string resource
You should always pick string from string.xml in res/values/strings.xml. It's not the right way to hard code the strings, putting it into the string.xml file helps for localization in future.
Add tools:ignore="HardcodedText" attribute
When you use tools, it doesn't actually set the text to view, but it just shows you in preview window as how it visually appears, when you run the app, you won't the text added in tools.
I'm implementing an app in Hebrew, and I like it to be user-friendly in such way that at the first time the user logs on, there will be a question "Are you a male or a female?". After answering this question, I want most of the strings to be gender-dependent
(E.g. in Hebrew the question "Would you like some coffee?" will be
תרצה לשתות קפה?
for a male, and -
תרצי לשתות קפה?
for a female)
Meanwhile my app supports English and Unisex-Hebrew Locales, so I'm using String resources (like R.string.somevalue) and I know how to handle values-he and values-en.
Let's say I can ask for is_male() and is_locale_hebrew() at anytime, I saw this answer but it won't help my case since there are a hell lot of strings in my working-already app and I want the solution to add only xml files (hopefully) with the less needed change in my "Activity"s code.
I thought maybe overloading the parser that looks for the xml files will do the magic, but I have no clue where to start from.
My question divides into two parts:
A. How can I implement gender-dependnt String-resources?
B. (Opt) Some of the string-resources are good as unisex right now, is there any option to avoid copying those resources to the 2 new gender-dependent files and just add a default behavior of "if you don't find a string resource at values-he-male search for it in values-he"?
Thanks in advance!
Re'em
Same question for me. I plan to have (in addition to the default string.xml in English) iw (for male) and iw_fe (for female).
When the user selects his/her gender, I will change the locale (Set Locale programmatically )
As for using default values in Hebrew, I am still clueless. For now I will simply copy the Hebrew XML and change the required entries, leaving all the rest intact.
HTH
Noam
I'm new to Android. When I add a button/views in Graphical layout it adds the label text this way- android:text="Button" . Why doesnt it add "android:text="#string/my_label" and add a string resource in string.xml file. Can't it be done automatically in eclipse?
I have searched a lot but I have not get any automated way to add a string to the resource file But This will save your time a lot IMHO.
Select a String, click Refactor --> Android --> Extract Android String.
Thanks to Brent Hronik. CTRL-1 on Windows works fine.
Because you don't have to use the #string resource. The purpose of the #strings resource is to make it easier to change elements about your code. For example, if you are using your application title in mutliple places, let's say in every dialog box, then if you change the title you would have to change it in all the instances that the app title is being display. So in this instance the #string/App_Title could be set to "My Program" and all of the dialog boxes can reference that. If you change the title to "Hello World" then all of these are changed. The #strings resource, while eclipse tries, doesn't have to be used for every string. Not using it is the equivalent to hard coding the value. There are plenty of reasons for and against using #string for everything.
I am not sure if there is a setting in eclipse that will automatically add a string to the resource file when the control is added.
(EDIT: Based on other users CTRL+1 is the short cut to do this.)
You can add the string to the strings.xml by clicking command and 1(on a mac, assume it would be control 1 on a Windows or Linux box) simultaneously. This will add the resource to strings.xml and then open that up in the editor.
Thanks Siddiq Abu Bakkar! I didn't think it would be there.
On Eclipse (and Windows) the shortcut is:
Alt+Shift+A (release all and then press) S
When you use Eclipse for first time it's not easy understand how to use these kind of "complex" shortcuts.
I can't vote and i can't comment answers yet (missing reputation as i'm a new user)
But i confirm :
1) hard type the string in your code like
mydlg.setTitle("hello guys");
2) select your string (e.g : "hello guys")
3) press Alt + Shift + A then press S
a dialog will appear to let you add a new string into resources. Everything should be already filled into that dialog box.
I wrote a big app with thousands of string in the code.... very bad idea, because now I want to translate each string.... big problem.
Copying all strings to the strings.xml takes a long time.
Eclipse has an option to take all selected strings and put them into messages.properties.
Does this work similiar like strings.xml? When, why all people use strings.xml.
Or should is use eclipse to seperate each string and than I should copy them to string.xml?
All people are using strings.xml because this is the normal way to do it on Android. You don't have to manage the load of the strings, to call any locale function in your script.
You can see the documentation here : http://developer.android.com/guide/topics/resources/index.html
BTW, you can easily transform your eclipse generated file to an strings.xml file after the extraction.
In Eclipse you can use the shortcut keys Alt + Shift A, S to extract an inline string in to the strings.xml file via a popup dialog - might be a bit easier than doing it by hand. And as the others say, yes you should ALWAYS use the strings.xml file so that you only have to look in one place when you want to change a string, instead of having to search through all your code.
Can I have Eclipse adding my string resources as I code or do I have to switch to string.xml all the time and add each string?
Eclipse will sort of do it for you. So if you have a field:
android:text="hello"
Select "hello" and then goto Refactor-->Android-->Extract Android String, Eclipse will change the line to:
android:text="#string/hello"
and automagically add the line to strings.xml as:
<string name="hello">Hello</string>
JAL
Eclipse has wonderful time-saving shortcuts for this!
1.- in XML editor:
Say you have a Button,TextView, or any View with a hard-coded string as text:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text to add as string resource"/>
With the cursor over the string literal, press CTRL+1, then choose "Extract Android String".
Choose the desired resource name (e.g. my_string_resource) then click OK. You will get a string resource in your strings.xml file:
<string name="my_string_resource">Text to add as string resource</string>
And your Button is now gonna look like:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/my_string_resource"/>
All automatically and without a single context-change :)
2.- In Code editor:
Write a string literal in your code like
mButton.setText("Text to add as String resource");
Then select the string literal (from " to "), and press CTRL+1, a yellow menu will appear, double click on "Extract Android String" (the S key does not work for me in this case, i just double click on the option). Choose the desired name (e.g. my_string_resouce), and click Ok. Again, you will get a new strings.xml entry:
<string name="my_string_resource">Text to add as string resource</string>
And your Button's setText line replaced by:
mButton.setText(R.string.my_string_resource);
Hope it helps and saves you as much time as it did for me! :)
The best practice is too have strings.xml inside values folder which keeps all string constants. Because later on if you want to make any change, it will easy for u if u keep in strings.xml. Otherwise you will have to always remember the file where u have wrote that constant.
You have to switch to string.xml: its unfortunate, but right now Eclipse doesn't give you a clean way of popping into the string editor directly from the code you are typing. Optimally you would type a string constant (like R.string.new_string and I guess hotkey or double click or something and jump directly into the strings.xml editor with the existing entry selected (if new_string exists) or a new entry created (if new_string doesn't yet exist).
Wouldn't that be nice.