Annoying default formatting - android

I am working on a simple android application in eclipse IDE and I got a little yellow icon on the left hand side of a line of xml code that looks like a light bulb with an exclamation mark beside it. When I hovered over, it says "[I18N] Hardcoded string "input..., should use #string resource input". The running and debug was successful but I just want to get rid of it as I find it annoying. What should I do?

If it's annoying, there is a reason. You totally should use #string resources instead of your hardcoded strings. All you have to do is to put your string in res/values/strings.xml and reference it in your layout via #string/my_string_id_here.
This is extremely useful for multi language support, or for plurals strings.
You can learn more here.
Hope this will help you.

The right way:
Move all your strings into resource files, as suggested, and reference them in your views like so: #string/mystringname
The "other" way:
Turn off Lint warnings in Eclipse in Window/Preferences/Android/Lint Error Checking
Both ways will remove that annoying triangle :)

This warning is there because hardcoding strings into the android app's Java source code is not recommended. It will compile fine - but Android Lint will complain about it, so that's why it's a "warning" and not an "error". Generally, it is preferable to define them in the separate "string.xml" file.
If you want to know why, check this answer.
For an example, check this answer.
You should also take a look at the official documentation for string resources.

Related

How to disable the preview of xml reference values in Android Studio settings/configuration?

I'm not sure if my question is worded correctly, but it's easier to show an image to explain my question.
In the XML editor, some values are replaced with the actual values that was defined for that entry. However, I don't want Android Studio showing me the actual values. Instead, I still prefer that it showed the reference names. So, how do you disable the editor from previewing in this manner?
In the image below, android:text="#string/sign_up", but the editor is showing it as android:text="Sign up...", as well as other attributes, and the reference name only shows up when I mouseover the attribute.
How to disable this feature permanently via Android Studio setting/configuration?
Update: Same thing is happening in .java files. Enums or R.string constants are being replaced by their actual values.
For Java files you can go to Preferences->Editor->General->Code Folding and uncheck "Android string references" but this only applies to code not to xml layouts
– Oleg Bogdanov
I fought with this for hours! Having the values show up instead of the references is great, in most cases, but I was working with a project imported from AIDE and its layouts were a mess. I need to see the references, not the values. I found the "possible duplicate" link above, mentioned by zombie, but it didn't work (or so I thought).
Finally, after reading this post, I tried unchecking the "Android string references" again, but this time I restarted Studio after (even though I wasn't prompted). If you have the same problem, the solution above should work, but save yourself (possibly) hours of aggravation and:
Restart Studio after unchecking the "Android string references" setting!
BTW, I would have added this as a simple comment, but SO wouldn't let me!

Does hard coding of string affect performance?

Whenever I make any app I always hardcode the string instead of referencing it from string resource from XML. App works fine but gives me warning to use #string resource
Example button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click here" />
My question is whether it will affect my application performance if I do so or it(#string resource) is just used for internationalization.
This is an Android lint warning to help you with localization.
Technically, hard coding strings would make your app perform a little better, as it won't have to look up the string from the corresponding R int each time. However, this performance difference will be negligible, and no human will be able to notice it.
However, you should always keep your String resources in the values folder as it makes localization very easy.
I see no reason that you would get worse performance by using a hard-coded string. There are fewer steps involved with hard-coded strings. However, it is certainly best practice to separate resource strings from application and UI code.
It will not create any performance issue. But defining strings in strings.xml is encouraged for the ease of maintainance and lolalization. For example consider the below two scenario.
Scenario 1
When you need to change a string used in many places. In your case You will have to change all the "click here" in all layouts. But if you declared in strings.xml then only change made in the xml will change them all.
Scenario 2
For another example if you want to show different language for different locale then you need to use the string.xml.
As others said, it's for localization,
but for performance, it depends on how many times per second those strings are getting looked up.
I've seen a case where an app is slow in startup, and stack sampling showed 50% of the time was being spent in resource lookup of strings, and the reason the strings were being looked up was to display them on the splash screen that gives the user something to look at while the app is starting up!
I dont think hardcoding a string will make your program to run any slower.. Infact it will enhance the performance as there's no need for any lookup for the String in the R.java class.
Referencing the string from strings.xml is best practice due to 2 reasons:-
1- Localization
2- if you're using the same string in multiple places and would like to edit the same in all the places saves you the overhead of editing all the hard-coded strings individually.
Hard coding strings will not affect the performance directly. It affects the maintainability.
In case when you hardcode a string and in later stage if you want to change the string "Click me" to "add" or something else, then you need to search your complete project to change the string where and all it is used. So Better to follow strings.xml always. :)
Your app will not support Localization then, if that is not the requirement of your app then there will be no problem using hard coded strings.

Differences between android:text="#string" and android:text="..."

Maybe this is a silly question but is there a difference,besides the obvious,between
android:text="#string/...." and android:text="..."?.I'm thinking that maybe the text that appears on the screen has the option for styling when using #string.Which one is best to use in general or it really doesn't matter?
When you're using android:text="#string/" the app is going to find the value of the string in the ressources file, with this technique you can manage multilanguage app, with a "strings"'s file by language.
Choice Matters, if your app has a lot of text in it, supporting a different language would be easier if you used #string, you would not have to scramble through every xml file to add text in the other language for every piece of text, simply go to strings resource and change there.
But then again, when checking UI for errors after work is done, it might make life harder (or work boring) as fixing a typo would require you to go to the xml file that the erroneous text is located, look up the name of string and then go to Strings to correct (unless you were very organised and named things well in your Strings such that you know which text belongs where)

Where can I see the default styles.xml in Android?

Where can I find the source code of the definition of the android default styles, such as ?android:attr/progressBarStyle?
I know that the Android repo is replicated in https://github.com/android but... I just can't find the styles.xml, strings.xml, etc there!
In the value resources directory.
With the ADT plugin in Eclipse, you can just type android.R.somegroup.someconstant, then mouse-over and hold CTRL (doesn't even have to be syntactically correct/compilable for that) -- this will give you a popup with two (or in case of strings.xml more) options to "Open declaration in [some XML file]".
For reasons that escape me, this doesn't seem to work for any of android.R.style.* -- for everything else it does (color, string, layout, ...).

Android app showing incorrect Spanish accent characters

I have a current Android app that uses i18n via resources. res/values-es/strings.xml and so on. When I test it on a device with the language set to Espanol it pulls the correct resources, from the values-es file, but the accent characters are way out of whack.
For example, I want to use the lowercase o with an accent (ó). I have tried with the actual character in the strings.xml file (using the character map on Ubuntu to create the string) and with the entity, in either case it comes out like some other character set accent I don't recognize:
The same character looks perfect WITHIN strings.xml when using many different text editors. And the file is UTF-8 (tried recreating it with the Android "wizard" tool in Eclipse to make sure).
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="label_app_version">Versión</string>
</resources>
Now I've used French, and German before in other Android apps, with all sorts of accents, and haven't seen this problem, so I'm entirely confused at the moment. What am I doing wrong this time?
I finally solved this one. I was using a font and calling setTypeface earlier in the code. The font I'm using must not have the special characters needed for the other languages. I need to check to make sure my user's are using a locale that my font supports before setting the font.
I should have realized this and checked it earlier.
So the bottom line is this, if you get strange results with certain characters in different locales, make sure you're not using fonts that don't support those characters. Fall back to not using a font (don't call setTypeface) and test that way.
That diacritic is actually called a breve, common in many eastern european languages. I had the same problems before once. I even have a question here on SO. Since nobody solved the problem, it's worth the shot...
This isn't clear to me yet:
Does it happen only on this project? Did you try others? (maybe a project mistake)
Try including other language folders (try, say, values-fr). Do you have the same problem?
Did you try to build your app inside Eclipse? Did you try building manually on the command line?
Then, you could try:
Instead of the "ó" (here in PT we use the same), try \u00f3. Like: Versi\u00f3n. Do you get the proper latin small o with acute? Just to clear things.
Build your app using ant ($ ant release) manually on the command line. Report back the results.
This may help the guys here (who are much more experienced than I am) guess where the problem can be.
Best regards!
Check how the actual strings.xml file is stored.
In the file tree in Eclipse, right-click on the strings.xml file and select properties.
You will get a properties dialog for the file.
If not selected already, select the "Resource" line in the left column.
Check the "Text file encoding" area at the bottom.
It maybe selected as "default" or a specific encoding such as "UTF-8" selected.
Select the UTF-8 option. Save file. Clean & Build project (to re-gen resources) and then see if any changes.
See attached screenshot.
Suerte, Andrew
This is strange as it should not cause a problem. Maybe you should try to embed string into CDATA section, like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="label_app_version"><![CDATA[Versión]]></string>
</resources>

Categories

Resources