using strings.xml with %, $ and / and formatted - android

I added this line to my strings.xml, and it wont compile into R.
<string name="cl_txt_verinfo">List Version: %1$s\nAuthor: %2$s\nDate Created: %3$s\nLanguage: %4$s</string>
the errors i get are:
error: Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?
error: Unexpected end tag string
I want to ask how to make this work(i will use string.format later)
and what does "formatted=false" do as opposed to not adding it/add it as true.
Edit:
Clean and build is how I got this error. I got it so many times.
But then I deleted an empty line and clean and rebuild everything is fine.
This is really inconsistent and i have no idea what's causing the problem in the first place.
Two questions left:
Could someone still tell me what difference would "formatted=false"
make?
How did I got this kind of behavior?(reporting error when there's none and i try to fix it by blowing things up and create a mess)
This is not the only time i had an error then it magically fixes itself. sometime i restart my adt and it resolves itself, but not this time sadly.

If you added formatted=false then format specifiers(%,$ etc.) in your string resource are disabled and Formatter won't be able to recognize them. This will definitely help you.
For your 2nd problem there is no exact solution but what I do is delete gen and bin folder and then clean build project. What happens is every time you build a project, android checks if there is any new resource available and adds it to R.java and only after that you can use it.

Related

"is translated here but not found in default locale" error in strings.xml with translatable="false"

Here is my values\strings.xml (the default file) and everything is self explanatory:
My question(s):
How can it be "not found in default locale" if I am editing the default locale (values\strings.xml)?
How can it be a lint translation error if I set translatable="false"? In the values-pl\strings.xml (and values-ru, values-iw folders as well) the strings don't exist even? it shouldn't anyway.
I can't seem to understand why I don't get the error for the Russian string.
(I would show the values-ru\string.xml or values-pl\string.xml here but there is nothing there of interest, since the strings are missing anyway...)
this happened to me too
I do 2 things:
Verify entire file to see another translate not done ( I have ones in file en but in pt-br)
Clean, update gradle and quit and restart the Android Studio
Rebuilding can take a long time. YMMV, but...
Simply remove the offending line and re-add it.
Worked for me. Very fast.
In my case this helped me:
Select the strings that cause error.
Cut them.
Paste them again
If it doesn't work for you, try adding the same strings to strings.xml(v21) file.
Press Invalidate Caches / Restart ... to restart android studio. It worked for me. Actually this happened when I had copied files from my other computer and pasted via file explorer.
I solved this issue by following below steps:
cleaning my project
rebuilding it
and finally pressing invalid caches/ Restart in file menu
If you just Clean Project it probably works. It worked for me.
I had the same error message, just with the weird issue that it was thrown in the default locale itself and all translations. Turns out you should not use dots in your name (e.g. name="bla.blub") because it will be internally converted to "bla_blub" and then it cannot match with "bla.blub", hence the error.
I only had to change the dots to underlines in the default locale and then all other errors in other translations (including dots in the name) were gone as well.
But be aware that other build tools can still create issues, so replace all the dots with underlines instead!
Quitting Android Studio and restarting it fixed it for me.
You simply just have to copy the offending line (or just one of the multiple of offending lines), remove it, and re-paste it. That removed the issue for me.
Just make sure that you have same naming conventions on all strings files including the same capitalization.
let's say if you have "sign_in" in strings.xml and "sign_In" in your fr/string.xml, so it will give you error on fr/strings.xml like "is translated here but not found in default locale". So, when you edit to "sign_in". The error will remove.
I was having this problem for all the strings in my xml file.
The thing is, your default strings.xml shouldn't have a tools:locale attribute as the other translated files have. If it does, whenever I compile the app in release mode, it treats it as another translated file.
I've created a tool to manage the translation status of android apps.
It not only tells you what strings are missing on the other languages, but it can also report, and clean the left over strings that you may have deleted on your default translation file.
https://github.com/gubatron/android-missing-strings
To clean all left overs in other languages invoke like this
./ams --cleanleftovers -o all.txt
This will clean the left over strings and it will output the missing strings report for all the languages into the all.txt file
Copy and paste didn't work for me.
I also tried Clean and restart, the previous error was gone but new entered strings still have new errors occur.
I tried closing the opened strings.xml file and Translations Editor, then restart.
strings.xml file and Translations Editor
That works for me.
Well, in my case, this happened when there was a format error with the previous string of the string that's being reported. Unfortuantely that format error was not reported. Correcting the format error solves the issue.
Add this,It will work
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ExtraTranslation">
Press double shift for open Translations Error and then check and uncheck in Untranslatable column the item with error.

My android app does not recognize R.string resources - what could be the reason?

I learn android with sample apps from textbook. In the given sample source R.string.something is recognized:
However, in my own EXACT source code, same R.strings are not recognized- highlighted as errors:
Does anyone know how to fix it?
I've had this many time with eclipse, and it was not a setup / code problem on my side. Generally, cleaning, rebuilding and sometime even stopping and restarting eclipse solved it. And yes, it's a pain in the neck ...
Try a clean of your project, if that does nothing then click on the problems tab and see if there's any build related issues, you may be missing a required jar or something!
These are following reason possible.
1-: You import android.R;
2-: Any error in xml files.
3-: Please check you String.xml file may be any error or declare a string more then two time.
I assume you are new to Android.
If so, there are two ways of using text strings in Buttons, textviews and so on:
1) Hardcoded string - you put the text you want in quotation marks (""), for example:
yourTextview.setText("Hardcoded string");
2) You can call the text from your String resources (res/values/strings.xml).
That is a much better approach seeing is is easier to translate, make changes and so on.
In your strings.xml file you can create all your string values, and call them from there.
Like in you example, if in your strings.xml file you have for example:
<string name="delete">This is String resource</string>
you can then call the string from there, like so :
yourTextview.setText(R.string.delete);
Hope this helps!

getResources().getString returns strange value

In my app I put a certain link in strings.xml file and then I use it in the rest of the application. Here is the way I put in strings.xml file :
<string name="link">http://mylink/</string>
and here in how I get it in my activity :
String link = getResources().getString(R.string.link);
The problem is that this String returns sometimes not the value from strings file. I get often #ff666666 and that's strange. Why sometimes it works fine and sometimes not ?
Has anyone any idea about this?
Thanks in advance.
You need to Clean and Build again before running the project.
This is happening because some times new values are added in the R.java file that doesn't get updated, so you need to clean to force the R.java file to refresh and update itself.
Try debugging into the particular method and inspect the value of R.string.link. Is it the same as you would see in the R.java file? Sometimes you get this problem when shifting around stuff in your resource files. "Project->Clean...->Clean all projects" in Eclipse usually solves it.
It is working fine. Just now i checked with following lines. Make sure sure to clean your project and build it.
<string name="link">http://www.google.com</string>
String name = getResources().getString(R.string.link);
Log.e("Name is:",name);

Why am I getting "can't find view" exceptions when the layout hasn't changed?

I'm working on one of my android widgets, which uses LauncherPlus to add scrolling functionality, and am running into a frustrating issue. My currently published version of the code, again using LauncherPlus, is working well, scrolling and all. I'm adding a requested feature, one which allows changing text sizes, but an exception is thrown when I test the update. There exception is:
mobi.intuitit.android.widget.SimpleRemoteViews$ActionException: can't find view:0x7f070041
I haven't changed anything in the layouts and the only code change references a view id which was already referenced in that same section of code. I looked through the R.java and found which resource was referred to and again, nothing has changed there. Here's the bit of code where the issue is coming from:
itemViews.setBoundBitmap(R.id.profile, "setImageBitmap", SonetProvider.SonetProviderColumns.profile.ordinal(), 0);
itemViews.setBoundCharSequence(R.id.friend, "setText", SonetProvider.SonetProviderColumns.friend.ordinal(), 0);
itemViews.setBoundCharSequence(R.id.created, "setText", SonetProvider.SonetProviderColumns.createdtext.ordinal(), 0);
itemViews.setBoundCharSequence(R.id.message, "setText", SonetProvider.SonetProviderColumns.message.ordinal(), 0);
The exception is thrown when applying the first line. Out of curiosity, I changed the order of these lines, and each one of them will cause the exception, though again, the layout hasn't changed. After the exception is thrown the widget will build successfully, but I can't publish this update with an exception (force close) being thrown. Any ideas why there is a resource issue? Thanks!
UPDATE:
I found this discussion where it seems that widget resources may not be reloaded on application updates:
http://groups.google.com/group/android-developers/browse_thread/thread/55a8e44974e8c6ad?fwc=1&pli=1
Does anyone have any experience with this, or a workaround? This may be what I'm facing.
Use Project > Clean in Eclipse or ant clean from the command line, then try again. Since resource IDs are integers, they are inlined in the bytecode of the classes that reference them, and so sometimes your pre-compiled classes can get out of sync with new resource IDs from a fresh compile. I work from the command line mostly, and I always tack clean onto my ant commands (e.g., ant clean install) to avoid this problem.

Content won't resolve

I'm working through a book samples programs and i get the same problem whether I create and type the code or whether i download the code already typed, so i'm thinking it's a setup problem.
I get this error in each each class:
setContentView(R.layout.menu) where the "R" is underlined in red and the code won't compile. If i add import android.r like "QuickFix" suggests, the red line goes away from the "R", but then the red line appears under "menu" (in this example). The same behavior occurs in each of the 5 classes that make up the project.
I do have matching layouts for "menu" etc. And (to the best of my knowledge) I registered them as Application Nodes in the manifest file.
It's driving me nuts. Thanks for any suggestions.
You are not supposed to import android.R as it won't resolve your resources. Just remove that import and try to compile again.
If you are working off a tutorial then also check to have the same resources as in the tutorial and also if you decide to name thing different then keep that in mind an reference them by your names.
The R class is generated by Android and contains IDs for all resources in your res folder. Go to your AndroidManifest and locate the package= attribute on manifest. R is in this namespace. So if the namespace is com.yourpackage.blah, you'll want to import com.yourpackage.blah.R;
The problem was that for some reason Eclipse was not autogenerating the gen/R.java file. I don't know why. One time it did generate the file, and a after Project..Clean and Build All everything resolved. Thanks for the input.
Also make sure your file names under res, eg drawables, are OK. I had a file name with capital letter and that prevented the creation of R with the same reference errors that you got. I did not check the error messages and it took me a good 10-20 mns to find this out.
Sometimes this happens to me.
It usually happens that I have been working with some xml element, and before clicking to a java document to hit "run" i just hit "run" while on the xml. I don't really know what happens when you "run" an xml, but it makes this horible file called some_xml_name.out.xml.
DELETE THIS FILE!
This file is the reason why you get so many R related errors, since the Resources library is never really created, or something like that.
That is my advice, without actually seeing the error you're getting in the console.

Categories

Resources