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
Related
I have a number picker and I can populate it with no problem from a regular array; however when I try to localize my app and place the array in the strings.xml file, that's when the frustration starts.
This is the strings.xml (partially)
<!--Set up arrays for the Number Pickers-->
<string name="pieces">Pieces</string>
<string name="kgs">Kgs</string>
<string name="lbs">Lbs</string>
<string name="oz">Oz</string>
<string name="gal">Gal</string>
<string name="liters">Liters</string>
<string name="pints">Pints</string>
<string name="quarts">Quarts</string>
<string name="fl_oz">Fl Oz</string>
<string-array name="all_units">
<item>#string/pieces</item>
<item>#string/kgs</item>
<item>#string/lbs</item>
<item>#string/oz</item>
<item>#string/gal</item>
<item>#string/liters</item>
<item>#string/pints</item>
<item>#string/quarts</item>
<item>#string/fl_oz</item>
</string-array>
Then in the MainActivity.kt, this is what I have:
val allUnits = arrayOf("Pieces","Kgs", "Lbs", "Oz", "Gal", "liters", "pints", "Quarts", "Fl oz")
npUnitsA.minValue = 0
npUnitsA.maxValue = allUnits.size -1
npUnitsA.displayedValues = allUnits
this works fine, but if I change that last line to:
npUnitsA.displayedValues = R.array.all_units
I get an error message:
Type mismatch.
Required:Array<(out) String!>!
Found:Int
Needless to say I'm very green at Android. Any help will be greatly appreciated.
R.java contains identifiers in int so its expected to give this error. What you can do here is
getResources().getStringArray(R.array.all_units)
to get the resource array
I need to know if there is a way to get String type data from strings.xml (nested in resource tag)
<resources>
<string name="app_name">One World</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="productInfo_lanIP">LAN IP Address :\t</string>
</resources>
and I need to get productInfo_lanIP from this and use it in my MainActivity.java to perform insertion of another string into this one.
Note: I am updating the value so I can use it to display it just after through an XML page. And I am using Android 4.2.2
you can get the strings like:
String productInfo_lanIP = getString(R.string.productInfo_lanIP);
go through this link
String mystring = getResources().getString(R.string.productInfo_lanIP);
Accept the answer if it helped you :)
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.
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.
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