I have a string-array in my string.xml file in Android res/value folder like this
my string.xml
<string-array name="books_titles_en">
<item>Smoking Effeccts on Your Body</item>
<item>Smoking - effects on your body</item>
<item>How Tobacco Smoke Causes Disease</item>
<item>patterns of use, health effects, use in smoking cessation and regulatory issues</item>
</string-array>
I need to retrieve the first element of array like this
my home.xml
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/frag_1_text_pad"
android:textSize="#dimen/frag_1_text_size"
android:paddingRight="#dimen/frg_1_text_right_pad"
android:id="#+id/book_link0"
android:text="#string-array/book_title_en[0]"/>
I know it seems like it will cause an error. How do I retrive these things on the string-array in string.xml in Android
Just to giva a full answer to your problem:
There is no way of accessing an array directly, but you could do something like this:
<string name="books_titles_en_1">Smoking Effeccts on Your Body</string>
<string name="books_titles_en_2">Smoking - effects on your body</string>
<string name="books_titles_en_3">How Tobacco Smoke Causes Disease</string>
<string name="books_titles_en_4">patterns of use, health effects, use in smoking cessation and regulatory issues</string>
<string-array name="books_titles_en">
<item>#string/books_titles_en_1</item>
<item>#string/books_titles_en_2</item>
<item>#string/books_titles_en_3</item>
<item>#string/books_titles_en_4</item>
</string-array>
and then:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/frag_1_text_pad"
android:textSize="#dimen/frag_1_text_size"
android:paddingRight="#dimen/frg_1_text_right_pad"
android:id="#+id/book_link0"
android:text="#string/book_title_en_1"/>
So you basically reference the neccessary strings directly, that are referenced in your string array. ;)
Refer to this link Android - retrieve string array from resources
And this link Help in getting String Array from arrays.xml file
Basically you will have to code for the array retrieval xml in the Java Class by making an adapter.
The first link I posted used
for(int i = 0; i < menuArray.length; i++)
{
Button b = new Button(this);
b.setText(menuArray[i]);
ll.addView(b);
}
which is a sample of how to obtain the whole array from the xml.
You can modify it for single array value retrieval, or whichever array value you wish to retrieve.
Happy coding :D
Related
<resources>
<string name="app_name">UnConv</string>
<string-array name="mainunit">
<item>Area</item>
<item>Pressure</item>
<item>Speed</item>
<item>Volume</item>
</string-array>
</resources>
i want to add some other values to the above items like a subgroup. Example for area i want yard, acre etc how can i achieve that?
Unfortunately, There is no direct way to save two dimensional string-array in Android resource file. I provide 2 methods for replacement.
1. Save the data as Json string showing in the following code:
<string name="mainunit">{"Area":["yard","acre"],"Pressure":[],"Speed":[],"Volume":[]}</string>
Then parse Json String to Java/Kotlin Object.
2. Save subgroup value splitting with ",", and get value by position, but it has drawback because it ignored the key name.
<string-array name="mainunit">
<item>yard,acre</item>
<item>Pressure</item>
<item>Speed</item>
<item>Volume</item>
</string-array>
I am using Android studio and have an array in my string.xml file as:
<string-array name="my_array">
<item>text1</item>
<item>text2</item>
<item>text3</item>
</string-array>
I know how to access the array (and get the 1st item) in my MainActivity.java file:
myButton.setText(getResources().getStringArray(R.array.my_array)[0]);
My question: Is there anyway to set the text directly in the activity_main.xml file? I tried:
<Button
android:id="#+id/myButton"
android:text="#array/my_array[0]"
... />
but that causes an error. Without the "[0]" it displays the 1st value (text1), but maybe that is just because of the button's size and it's not showing the rest - I can't get it to display other items (e.g., text2).
Is it possible to access one value of the array directly in the layout file?
Thanks.
I found a good answer here: https://stackoverflow.com/a/4161645/933969
Basically you create named strings first (and use those where you would want mystrings[x]) and then create your array using references to those named strings:
<string name="earth">Earth</string>
<string name="moon">Moon</string>
<string-array name="system">
<item>#string/earth</item>
<item>#string/moon</item>
</string-array>
I know how to get data from string-array. I have done like this.
<string-array name="values">
<item>value1</item>
<item>value2</item>
</string-array>
String[] values = this.getResources().getStringArray(R.array.values);
But I dont know whether the below type string-array is possible and confused to get values from that.
<string-array name="country_data">
<item>
<country>Afghanistan</country>
<countryCode>93</countryCode>
<iso2>AF</iso2>
<iso3>AFG</iso3>
</item>
</string-array>
If it is possible please help me to get the values.
I dont know whether the below type string-array is possible
It is not.
If you want to have arbitrary XML, use res/xml/ for whatever XML structure you like. You can then use getResources().getXml() to get an XmlPullParser that you can use to parse that XML.
Or, wrap the contents of each of your <item> elements in CDATA, which should prevent the resource parser from messing with them. Then, each string you retrieve from the array would be plain XML, that you would need to parse.
Or, go with JSON in res/raw/ or assets/, using your favorite JSON parser (e.g., Gson).
Or, use SQLiteAssetHelper to package a database in your app that has this data pre-loaded.
Or, use R. Zagórski's solution, of having one <string-array> per object, with <item> elements for each field of the object.
xml :
<string-array name="_name">
<item>n11</item>
<item>n12</item>
<item>n13</item>
<item>n14</item>
<item>n15</item>
<item>n16</item>
</string-array>
Retrieve at java class:
String[] test;
test = getResources().getStringArray(R.array._name);
Of course it is possible.
In summary:
Define POJO representing your model
Create a function to import string array of arrays from resources into this POJO
Remember that you cannot use custom xml entries, they have to be <item>. And you need to know it's type upfront.
I want to associate URLs with items in a string array so that when an item in a ListView is selected data is scraped from the URL. I was wondering could the URL be added as metadata in the strings.xml file? Does anyone have any suggestions as to how to proceed?
Strings.xml isn't the right place. You COULD define the URLs, one at a time, in strings.xml, like this:
<string name="url1">http://foo1.com</string>
<string name="url2">http://foo2.com</string>
// etc
And then create an array at runtime, dumping all these values in one-by-one. But that would suck.
Instead, create an xml file in res/values/, name it whatever you want (for instance, urlvals.xml), and populate it like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="urls">
<item>http://foo1.com</item>
<item>http://foo2.com</item>
</array>
</resources>
Then, in code, reference it in the following way:
String[] myUrls = getResources().getStringArray(R.array.urls);
If you want an array of string resources, you can either:
1) add the strings in strings.xml (as per #Alexander's answer), and reference them by name in an array in values/yourresourcename.xml as per here.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="myLinks">
<item>#string/link1</item>
<item>#string/link2</item>
</array>
2) add them directly the string array (verbatim) like #Alexander suggests
How you associate them with the elements in the ListView is up to you. I would suggest (if the content is static) creating a matching string array for (e.g.) the string you want to actually display (as opposed to the link itself).
Ok, so this question is a bit weird, and kinda rubs me the wrong to even ask, but since I probably don't have a choice I want to see what you guys think. I'm still a novice at Java and Android.
The case is as follows: We are trying to automate the building of our strings.xml for localisation. A parser has been made to convert a csv-file to xml. For regular strings it's not a real problem, that works fine. But the parser that was built, doesn't take string arrays into account and there is little chance that someone will modify it.
Is there an "easy" way to work with the strings and create an string array programmatically based on parts the name attributes?
If not, then I would have to hard code the arrays and that leaves the creator (client) of the language files unable to add items to something that should be a dynamic list.
I know modifying the strings.xml manually might be an option, but because our management wants to automate stuff like that, it's not much of a choice I have.
Probably I will hard code the stuff and say they can't dynamically fill the lists, but still (also for my personal education) I wanna know what you guys think.
Thanks for your opinions or solutions. :)
Cheers!
You can use (& really really really should anyways) references in string arrays. Assuming this is your generated res/values/strings.xml in Swedish:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="string1">Hej, detta är på svenska</string>
<string name="string2">Denna strängen också</string>
</resources>
You can put your string-array in, for instance, res/values/arrays.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="somearrayname">
<item>#string/string1</item>
<item>#string/string1</item>
</string-array>
</resources>
So you will get strings.xml as below
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="string1">1st string</string>
<string name="string2">2nd string</string>
........
<string name="stringN">Nth string</string>
</resources>
I suggest a simple modification to strings.xml, which is add a string with name count at top of all strings with value equal to total number of strings and add another string with name prefix below the count, then it looks like as below.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="count">4</string>
<string name="prefix">string</string>
<string name="string1">1st string</string>
<string name="string2">2nd string</string>
<string name="string3">3rd string</string>
<string name="string4">4th string</string>
</resources>
So now, in your java file you can access them like below
int count = Integer.parseInt(getString(R.string.count));
String prefix = getString(R.string.prefix);
String[] strings = new String[count];
for (int i = 0; i < count; i++) {
strings[i] = getString(getResources().getIdentifier(prefix+(i+1), "string", getPackageName()));
}
I hope it may help you.