How to access strings declared inside a particular resource file in android - 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?

Related

Android resource for a specific product

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.

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 =)

Internal string resource references

I wonder if it's possible to reference a XML string value in another XML String resource.
Things like the following are possible:
<string name="title">MyTitle</string>
<string name="activityTitle">#string/title</string>
But in case of an concated resource string, I found no solution yet, so what I'm searching for is the following:
<string name="title">MyTitle</string>
<string name="subTitle">#string/title - mySubTitle</string>
So far I was only able to solve it programmically via:
<string name="title">MyTitle</string>
<string name="subTitle">%1$s - mySubTitle</string>
getResources().getString(R.string.subTitle, getResources().getString(R.string.title));
I would like to keep the string references in the string.xml file itself.
What you want is not supported at this time, AFAIK.

Can one combine android resource strings into new strings?

Android allows to create aliases of resource strings like:
<resources>
<string name="foo">somestring</string>
<string name="bar">#string/foo</string>
</resources>
by which the resource "bar" becomes an alias for the resource named "foo".
What I would for my app is a possibility to combine an existing resource prefix with different suffixes, i.e. to extend it like:
<resources>
<string name="foo">foo</string>
<string name="bar">#string/foo+bar</string>
<string name="else">#string/foo+else</string>
</resources>
where the resource "bar" would yield the string "foobar". Its clear that '+' doesn't work here but is there some other option to achieve such a string concatenation, so that one could define a bunch of string resources that have a common prefix?
I realize of course that I could do such resource string concatenation at runtime but defining them statically in the resources would seem so much more elegant and simpler.
Michael
No, it's not possible.
You can just use <string name="bar">This is my %s</string>
and then use String.format() in your app to fill in the variable with for example
getResources().getString(R.string.foo);
I know it's late, but anyways now you can do so by using this library I've created: https://github.com/LikeTheSalad/android-stem
It will concat all of the strings you want to at build time and will generate new strings that you can use anywhere in your project as with any other manually added string.
For your case, you'd have to define your strings like so:
<resources>
<string name="foo">foo</string>
<string name="bar">${foo} bar</string>
<string name="else">${foo} else</string>
</resources>
And then, after building your project, you'll get:
<!-- Auto generated during compilation -->
<resources>
<string name="bar">foo bar</string>
<string name="else">foo else</string>
</resources>
These auto generated strings will keep themselves updated for any changes that you make to your templates and values. They also work with localized and flavor strings. More info on the repo's page.

Categories

Resources