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.
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.
[I18N] Hardcoded string "Happy Birthday Debashish", should use #string resource less... (Ctrl+F1)
Hardcoding text attributes directly in layout files is bad for several
reasons: * When creating configuration variations (for example for landscape or
portrait)you have to repeat the actual text (and keep it up to date when
making changes) * The application cannot be translated to other languages by
just adding new translations for existing string resources. In Android Studio
and Eclipse there are quickfixes to automatically extract this hardcoded string
into a resource lookup.
Ths is not an error but a warning. As a general rule, you should never use hard-coded strings in your layout but always use string resources instead (which means that all the strings are stored in one separate file where they are easily changeable for different languages and so on).
To convert a hard-coded string into a string resource:
Put the curser on the hard coded string;
Press ALT + Enter;
Enter a name for your ressource;
Click OK.
After doing this the warning will be gone.
This is just a warning.
Define your string in string.xml file
Happy Birthday Debashish
and in textView use this string as
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/string_name"
/>
This is only a warning. The function will still work as intended. It is just recommended to place your text in the strings.xml file. This makes future changes much simpler and is easier to reference across multiple pages.
First, place the <string> element in values/strings.xml like this:
<string name="your_string_name">Happy Birthday Debashish</string>
Then, you can reference the string in the .xml file as follows:
<TextView
android:text="#strings/your_string_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Again, it is not required to do it in this method. It just makes things simpler to manage and change in the future if needed.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title"
tools:text="Happy Birthday Debashish" />
When you are in the 2019 version. Go to the strings.xml and Add this in to it
<string name="Your text">Your Text</string>
Or
In the warning it has the Fix button you can click it for fix
Everybody knows that if we have:
ekran3.setText("VAT Tax:");
We may (or even we SHOULD) convert it to:
ekran3.setText(getString(R.string.kwotaVat));
and add in strings.xml:
<string name="kwotaVat">VAT Tax:</string>
But is there some kind of trick to do it automatically? For example by clicking RMB on text and selecting some option? It would be nice to know it in fact it will save us a lot of time than while we're doing it manually.
If you are using Eclipse you may extract the string directly into the strings.xml file by placing the mouse within the string and hitting Ctrl + 1. It will bring up the dialog as followed and you may select "Extract String". You then give it a name (Ex: kwotaVat) and you're done.
hey you do not need to use getString() to convert it to string the values xml file is already having data in string form so you just need to use the following code to set the string
ekran3.setText(R.string.kwotaVat);
where ekran3 is the object of your text view
and kwotaVat is the id of your value string
for more detail od android codes have look here http://grabcodes.blogspot.com/
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" />
I find it a waste of time that every time I need to enter a string that I have to go edit the strings.xml file manually. What I'd like to be able to do ideally is have Eclipse pop up a dialog box that lets me specify the name of the resource and the value for it. Then Eclipse would generate the code in strings.xml and paste the correct reference to the string at the cursor position.
Does such a facility exist?
Select a String, click Refactor --> Android --> Extract Android String.