Keeps String values separated in Android Resources - android

As you know you can write whatever resource you need inside res/values.
I made an xml file for each gui/activity I'm using. Here's an example:
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">This is home!</string>
<string name="app_name">Hattrick4Manager</string>
<string name="oauth_button">Authorize me!</string>
<string name="download_button">Syncronize</string>
<string name="oauth_name">Authorization</string>
</resources>
update-ui.xml
<resources>
<string name="inizio_download">Update Started...</string>
<string name="fine_download">Update Completed...</string>
<string name="update_title">Update</string>
<string name="update_done">Done</string>
<string name="update_inizio_club">Club...</string>
<string name="update_inizio_arena">Arena...</string>
<string name="update_inizio_fan">Fans...</string>
<string name="update_inizio_matches">Matches...</string>
<string name="update_inizio_league">League...</string>
<string name="update_inizio_leaguefix">League Fixtures...</string>
<string name="update_inizio_economy">Economy...</string>
<string name="update_inizio_players">Players...</string>
</resources>
When I use this in the code I have to recall them like:
R.string.update_done
or
R.string.hello
my problem is that like this I have basically to add the prefix for every GUI I make. I would prefer doing something like:
R.string.update-ui.done
Is it possible?

You can create as many resource files as you want, but you can not perform the nested name referencing based on the filename: the Android aapt tool doesn't support arbitrary nested objects on the generated R object.
You might be able to do stuff like this in update-ui.xml:
<resources>
<string name="update_ui_inizio_download">Update Started...</string>
<string name="update_ui_fine_download">Update Completed...</string>
</resources>
and then use
R.string.update_ui_inizio_download
to reference the contents of the file

Related

strings.xml: Error: Found item String/button more than one time

strings.xml: Error: Found item String/button more than one time
how to rid off this problem??help
You cannot have multiple strings with same name ("button") in your case. Give unique name to each of them as follows
<resources>
<string name="app_name">CourtCounter</string>
<string name="button1">Button1</string>
<string name="button2">Button2</string>
<string name="button3">Button3</string>
</resources>
Do like this:-
<resources>
<string name="app_name">CourtCounter</string>
<string name="button1">Button1</string>
<string name="button2">Button2</string>
<string name="button3">Button3</string>
</resources>

R. not giving me array option

Im trying to read an array from my strings.xml to my code but R. is not giving me the array option, maybe it has something to do with the package? for example when i tried android.R it gave me a array option but I couldnt find the name of the strings-array object i created in xml.
Here is my code:
this.context = context;
Resources res = context.getResources();
thumbnailNames = res.getStringArray(R.array.thumbnail);
here is my xml file:
<string name="app_name">Wallpapers: Art of Autism</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="donate_sidebar">Donate for Charity</string>
<string name="who_sidebar">Who are we?</string>
<string name="mission_sidebar">What is our mission?</string>
<string name="autism_sidebar">What is autism?</string>
<string name="developer_sidebar">About the developer</string>
<string name="categories_main">Categories:</string>
<string name="aoaart_main">AoAart</string>
<string name="featured_radiobtn">Our Favorites</string>
<string name="abstract_radiobtn">Abstract Art</string>
<string name="scenery_radiobtn">Scenery</string>
<string name="animals_radiobtn">Animals</string>
<string-array name = "thumbnail">
<item >one</item>
<item >two</item>
<item >three</item>
<item >four</item>
</string-array>
Try this way,hope this will help you to solve your problem.
When you import android.R which provide android default resource.
In you case you have define your custom array in string so you can access your custom resources using YourPackageName.R reference so try to import YourPackageName.R instead of android.R
Found the answer Can't get array string resource from XML
wasnt compiling, so i just changed the name of my strings.xml file and clean build and then changed it back and clean build

How can I nest string resource?

This is what I want to do, in standard way.
<string name="foo">foo</string>
<string name="foo_bar">foo bar</string>
Can I rewrite like below?
<string name="foo">foo</string>
<string name="foo_bar">#string/foo bar</string>
I know this is not in schema, however, it is OK like below,
<string name="foo">foo</string>
<string name="altfoo">#string/foo</string>
Why!?
This
<string name="foo">foo</string>
<string name="foo_bar">#string/foo bar</string>
wrong coz there is no resource with the name foo bar
The above would be right if you had
<string name="foo bar">foo and bar</string>
<string name="foo_bar">#string/foo bar</string>
This
<string name="foo">foo</string>
<string name="altfoo">#string/foo</string>
is right coz there is a resource with name foo and you are referencing the same
You may want to check
Concatenate Strings in the strings.xml file for Android

Hebrew language not showing - Android

I have created 2 string files one the default under res/values/strings.xml and the other under res/values-he/strings.xml. I am opening my app, while the os is configured to be in Hebrew, but still I see the values of the default strings which are in English. Is there any thing more to do?
Note: in the eclipse graphical layout editor I can choose Hebrew and see that it works fine.
Here is some of my default strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyApp</string>
<string name="title_activity_splash">MyApp</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_login">Login</string>
<string name="email">Email</string>
<string name="password">Password</string>
<string name="forgot_password">Forgot password?</string>
<string name="sign_up">Sign up</string>
<string name="login">Login</string>
.
.
.
and here is all (for now) my hebrew strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="action_settings">הגדרות</string>
<string name="hello_world">שלום עולם</string>
<string name="email">דואל</string>
<string name="password">סיסמא</string>
<string name="forgot_password">שכחת סיסמא</string>
<string name="sign_up">רישום</string>
<string name="login">התחברות</string>
</resources>
Here it in eclipse:
here it on the device (device os in hebrew):
OK, you correctly have:
res/values/strings.xml
and
res/values-he/strings.xml
With the correct structure, be also sure that the strings have the very same names in each strings.xml file, or the trick won't work.
The missing strings in a translation file won't be translated and the default strings (from res/values/strings.xml) will be used instead.
For hebrew, in particular, on some devices values-he is recognized. in some others it's values-iw.
So, make a copy of values-he and name it values-iw. Keep both in your structure.
What solved the problem for me was to use the folder values-iw.

Android strings.xml not in R.id

I am unable to see my values in my strings.xml
When I try to see it in R.id it doesnt exist
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">BPhonelist</string>
<string name="action_add_contact">Add Contact</string>
<string name="action_phone_contact">Phone Contact</string>
<string name="action_work_contact">Phone Work Contact</string>
<string name="action_home_contact">Phone Home Contact</string>
<string name="action_email_contact">Email Contact</string>
<string name="hello_world">Hello world!</string>
<string name="detail_toast_add">Contact Added</string>
</resources>
Please if you know how I can get access it tell me.
When I try to see it in R.id it doesnt exist
that's because these are in your strings.xml. They aren't id resources. Try accessing with something like
String someString = getResources().getString(R.string.app_name);
This is assuming you are already in the Activity Context. If not, you will need to have a Context before getResources()
R.id.* values will be generated from layout files using android:id="+#id\myid"
You can reference your strings via the R.string. prefix.

Categories

Resources