Android resource for a specific product - android

i got this code from Settings app...
<string name="about_settings" product="tablet">About tablet</string>
<string name="about_settings" product="default">About phone</string>
then my questions are:
from where at runtime the system load the correct string resource ?
What must I do to add a new product? e.g.
<string name="about_settings" product="laptop">About laptop</string>

from where at runtime the system load the correct string resource ?
The system does not load this at runtime. The correct string resource is preloaded according to PRODUCT_CHARACTERISTICS defined for a specific target build. So you cannot use this while building from eclipse. This is used only for building apps preloaded on platform.
2 . What must I do to add a new product? e.g.
You need to add to PRODUCT_CHARACTERISTICS in device.mk file

Answer for your question 1.
You can get them via
String mystring = getResources().getString(R.string.mystring);
But your strings.xml should something like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="tablet" >About tablet</string>
<string name="default" >About phone</string>
</resources>
Answer for your question 2.
How will that work, because that is a resource file. If you want you can either create a file and save the information in it. Locally. But it will be only on that device.

Related

Strings.xml gives a translation error whenever I try to add a new string resource

The following is the only error message when i try to add a new string resource.
NOTE : I'm not doing any localization, there is only default values folder and adding string in strings.xml file only using extract string functionality of the android studio from my xml layout.
"string_name" is translated here but not found in default locale.
In newer Android Studio (v.3) any string named with dot will cause "... is translated here but not found in default locale" error.
If your strings.xml has something like this
<string name="aaa.bbb">cccc</string>
you need to change it to
<string name="aaa_bbb">cccc</string>
I did invalidate cache & restart, which solved my issue.
Syncing project with gradle files solved my issue.
On the top of the resources file add this:
<resources tools:ignore="ExtraTranslation" xmlns:tools="http://schemas.android.com/tools">
Worked for me.
You should add a default string "string_name" to the strings.xml file.
strings.xml
<resources>
<string name="string_name">default Test</string>
</resources>
Check you translate in Translations Editor

How to access strings declared inside a particular resource file in android

The idea is to read all the strings declared inside a strings.xml file named strings_temp.xml for example, that has the following in it:
<resources>
<string name="string_one">String one</string>
<string name="string_two">String two</string>
</resources>
and this string resource file exists in values-ru also (so we have localization).
I have searched on StackOverflow and i know we can read through all the strings in the app but i do not want to go through so much of processing just to lookup for a particular string whose id and value i may not know (it is all dynamic).
What should i do in such a case?

Compare Android String xml files

I have two string xml for two different languages, I would like to know the different between those xml files.
For example, there is one xml for English,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Keep Accounts</string>
<string name="insertNewOne">Insert Accounts</string>
<string name="browseRecord">Browse Records</string>
<string name="set">Setting</string>
</resources>
And another xml for other language,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Example</string>
<string name="insertNewOne">Example</string>
<string name="browseRecord">Example</string>
<string name="dateNoColon">Example</string>
</resources>
We can see the difference is xml for English has element string name="set", and the other has not. On the other hand, the xml file for other language has element string name="dateNoColon" but the xml for English has not.
In this case, I would like to know the English xml lacks the element string name="dateNoColon", and other xml lacks the element string name="set".
Android Studio has translations editor starting of 0.8.12 version. You can find there missing translation strings.
You can enable check for missing translations in Lint tool. There are "Missing translation" and "Extra translation" checks.
Extra translation If a string appears in a specific language translation file, but there is no corresponding string in the default locale, then this string is probably unused. (It's technically possible that your application is only intended to run in a specific locale, but it's still a good idea to provide a fallback.).
Incomplete translation If an application has more than one locale, then all the strings declared in one language should also be translated in all other languages.
Suppose if the device is set to Other language, Android will look for title in the otherlanguage.xml file in value folder. But if no such string is included in that file, Android will fall back to the default, and will load title in English from the english.xml file.
For more detail go to http://developer.android.com/guide/topics/resources/localization.html#using-framework
I wrote a small tool for that: resdiff.
Check it out! https://github.com/danijoo/resdiff
Try sorting both files using some perl or bash script or something like that for example, using bash:
sort temp.txt -o temp.txt
and then look at the diff for example using DiffMergeit.
Use Android Lint to find both incomplete translations i.e. strings missing in a language variant and extra translations i.e. strings introduced in a language variant but missing in the default locale.
In Android Studio you can run Lint (and some other analysis tools) with Analyze -> Inspect Code.

pseudo-resources (using configuraton qualifiers) at runtime

I am creating an app which utilizes images from local device folders (brought back from server) at runtime rather than from the app's own resources folders.
I would like to take advantage of Android's runtime ability to use the appropriate resource files for different display types/orientation, similar to its present runtime usage of different resource folders (ldpi, hdpi, sw600sp etc) but in my case, not actually with complete resource folder content present during the app build.
Which would be the preferred methodology to achieve this?
i) pulling back the server images files and creating a sub-directory structure recognized by Android in a similar fashion as it presently recognizes its res directory structure -- i.e. a runtime pseudo-resource folder if you like.
ii) overriding events to catch orientation changes on existing activities and manually determining display type at start-up, in order to point all the app's activities to appropriate (locally stored at runtime) image files.
I am not entirely sure how to achieve either of the above, hence any indication of preference and general approach would be appreciated.
You can use Android's resource identification mechanism for this too. You can create the different images on your server and you can store the links to this in your strings.xml
So you will have different strings.xml in different res/values-xx folders (where xx stands for the configuration qualifiers). Take a look at Providing Resources to know more about the configuration qualifiers.
so in values-ldpi, your strings.xml can have a value as:
<string name="icon">http://my-server/images/icon-ldpi.jpg</string>
whereas the strings.xml in values-hdpi will have the value as:
<string name="icon">http://my-server/images/icon-hdpi.jpg</string>
and so on.
Edit:
In case the url contains runtime parameters, you can specify the same in the strings.xml as:
<string name="icon">http://my-server/%1$s/images/icon-ldpi.jpg</string>
And you use the same in the Java code as follows:
String iconURL = getResources().getString(R.string.icon, siteId);
If the siteID is 001, depending on the device type, the imageURL would be "http://my-server/001/images/icon-ldpi.jpg" or "http://my-server/001/images/icon-mdpi.jpg" or "http://my-server/001/images/icon-hdpi.jpg" or ... And the fact is that you don't have to do anything in your code to determine the display attributes for this to work.
This has turned out to be easier than I thought. I can just return 'identifier' strings stored in assorted values-xxx folders in order to build an appropriate set of possible file names.
e.g.
In values\string.xml:
<resources>
<string name="app_name">Resource Tester</string>
<string name="action_settings">Settings</string>
<string name="myindicator_lang">en</string>
<string name="myindicator_dpi">normal</string>
<string name="myindicator_orient">port</string>
</resources>
In values-de\string.xml:
<resources> <string name="myindicator_lang">de</string> </resources>
In values-land\string.xml:
<resources> <string name="myindicator_orient">land</string> </resources>
In values-hdpi\string.xml:
<resources> <string name="myindicator_dpi">hdpi</string> </resources>
etc.
With this string info returned at runtime, I can just dynamically build the required file name based on device configuration, allowing me to store alternative server/local storage resources with an appropriate name and have my app utilise the most appropriate image file if it exists.
i.e. in server/local storage have:
mybanner-en.jpg
mybanner-de.jpg
mybanner-de-land.jpg
etc.
I can now search for an existing stored file which best matches the device config at the time.
Many thanks Rajesh, for pointing me in the right direction.

Can we create new_strings.xml with strings.xml in values folder for sting strings (Android)?

I have two xml files in values folder for strings:
new_strings.xml
strings.xml
From strings.xml I can access string as follows:
String str = getString(R.string.app_name);
How can I directly access from new_strings.xml?
By the same way you're accessing the values in strings.xml file.
Example :
strings.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name_1">First app name</string>
</resources>
new_strings.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name_2">Second app_name</string>
</resources>
In your java code you can do :
R.string.app_name_1
R.string.app_name_2
and you can access both values which are in two different xml files.
As the doc said:
file location:
res/values/filename.xml
The filename is arbitrary.
The <string> element's name will be used as the resource ID.
compiled resource datatype:
Resource pointer to a String.
To give you a really short answer:
Yes, you can access to all strings in both files regardless of the filename. Although it's generally a good practice to keep all strings in one file, I do personally find it useful to separate strings into multiple files, especially for localization purposes, which brings me to the next point:
Yes you can access to the localized strings in other locale folders, e.g. values-es, as long as it contains a translated copy of all string files in the values folder.
For example, if you have strings.xml and new_strings.xml in your values folder, make sure you also have both of those files in your values-es folder in order for your app to display the localized strings in Spanish.
Hope this clarifies things up for you =)

Categories

Resources