How to solve this issue of Hardcoded string? - android

[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

Related

Why does Android Studio give me warnings to add code that Android Studio already added automatically?

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.

How can I write "ó" in Android Studio on layout?

I am intented to write Iniciar sesión, but the ó not working. For this reason I changed to ó but is another error.
<TextView
android:id="#+id/textView"
android:layout_width="330dp"
android:layout_height="24dp"
android:text="Inicio de sesión"
tools:layout_editor_absoluteX="27dp"
tools:layout_editor_absoluteY="28dp" />
¿I must configure Android Studio?
I added compileOptions.encoding "ISO-8859-1" in build.gradle but the error persist.
Help me, please.
Thank you!
To fix this issue, you have to create a Strings.xml file and add to it and qualifier (Spanish language) then you can add this letter with accent without any problem
<resources> <!-- spanish language qualifier -->
<string name="test_text">Iniciar sesión </string>
</resources>
You can read more about qualifiers in the oficial documentation here:
Qualifiers
You should avoid to use text like this, directly on your layout file. Instead of it, you should relay on the file string.xml where you define all the texts your xml files will need to display. By doing this, you will be doing nothing less than the correct specification from Android makers in order to localize your app (more info on this: https://developer.android.com/guide/topics/resources/localization.html ).
So, proceed this way:
1) on your res folder there is another one called values. There you will find your strings.xml file. Open it and create a new key, let's call it txtarriba, like this:
<string name="txtarriba">Begin of session</string>
2) with android studio or your regular file manager, create another folder below your res folder, call it values-ES.
3) inside values-ES create a new file named strings.xml, there you will insert the translation of that key txtarriba. So do this:
<string name="txtarriba">Inicio de sesión</string>
4) Then, on your primitive layout xml file you define your textview like this:
<TextView
android:id="#+id/textView"
android:layout_width="330dp"
android:layout_height="24dp"
android:text="#string/txtarriba"
tools:layout_editor_absoluteX="27dp"
tools:layout_editor_absoluteY="28dp" />
DONE.
With this you can assure you app is, from now on, correctly showed according to your device idiom. If your device is in english, Begin of session will be showed. If device is set with spanish language, you will read Inicio de sesión. With this you can forget all the troubles about special chars, Android manages this for you ;)

error in android:text

i am writing a piece of code where it creates a button and put a text on it. here is the piece of code
<Button android:id="#+id/start_client"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Client" />
but the part "android:text="Start Client" />" is being highlighted (yellow), not allowing me to build the project.
Can anybody help me out??
Thanks
//that yellow line is indicating as warning
you need to use res/string file to store text values.
[I18N] Hardcoded string "Start Client", should use #string resource
this is because of Lint tool.
your project has some other error.
open your problems tab and check whats wrong there.
//clean and build again will also work fine.
The yellow part is a warning to you to not use a hard coded string for text. means you cant give just any string directly to any content like button.
whatever string you want to use, first you will add that in string.xml (check in "values" folder in left side).
after adding any string, you can use in you project anywhere without getting any warning.

How to add string resources in Eclipse?

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.

XML within an Android string resource?

I was wondering if I could place XML within /res/values/strings.xml? I ask this because I am checking for the XML data file for my application, if it does not exist yet then it creates it from the default contents that will be contained as a string resource.
Eclipse tries to change the less than and greater than tags to their corresponding HTML entities when using the GUI to edit the strings. Is eclipse on the right track? Because I should think that it will be written out into my file as HTML entities too. Could I use getText() rather than getString() to convert the entities back into tags?
Yes you can, just use CDATA
<string name="stringName1"><![CDATA[<html>bla</html>]]></string>
It will obviously not work unless you escape characters in there such as < or > or &.
If you do encode the XML, it should work fine but probably not the best way to do it. I would prefer binary resource.
For putting in string.xml, you may encode using
String encoded = URLEncoder.encode(xml);
And decoding is the opposite.
For binary, you place it in RAW folder and you get a binary stream and turn it to string and load.
I have done this way:
Put your string in strings.xml
<string name="my_string"><![CDATA[Your long text here]]></string>
How to use:
<TextView
android:id="#+id/textView"
android:text="#string/my_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Done

Categories

Resources