Reference a specific item in a <string-array> from a preferences XML? - android

Assuming I have a string array defined as:
<string-array name="settings">
<item>not very unique</item>
<item>not very unique</item>
<item>slightly more unique</item>
</string-array>
Is there a way to reference an item, by its index in the array, from a preferences XML?
I know how to reference the entire array:
android:entries="#array/settings"
But is there a way to reference only one item from the array?

Well no, there is no way, but you could do something like this:
<string name="mystring_1">awesome string is awesome</string>
<string name="mystring_2">even more awesome string</string>
<string-array name="mystrings">
<item>#string/mystring_1</item>
<item>#string/mystring_2</item>
</string-array>
So you basically reference another string in your resources, that is referenced in your string array. ;)

Related

How can i create nested array in android studio Strings.xml

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

How to get the count of array from resources file?

I have multiple arrays in resources like this:
<resources>
<string-array name="myArray1">
<item>String1</item>
<item>String2</item>
<item>String3</item>
</string-array>
<string-array name="myArray2">
<item>String1</item>
<item>String2</item>
<item>String3</item>
</string-array>
<string-array name="myArray3">
<item>String1</item>
<item>String2</item>
<item>String3</item>
</string-array>
</resources>
Now how to get the count of these array dynamically?currently I have 3 arrays.
I can get the particular array items like this
String[] resourceString =getResources().getStringArray(R.array.myArray);
But how to get the count?
int length =getResources().getStringArray(R.array.myArray).length;
The XML file is not for counting the arrays inside it. It is for storing information that doesn't belong in your code in a static way.
This way you can alter menu's, lists dynamically.
What you can do is instead of creating multiple arrays, is store it ** multidimensional**. link
Now you can count the arrays like user1969053 is suggesting

Reference a resource from another resource

quick question my SO/documentation-checking has failed to answer:
Is it possible to have the value of a string-array resource be a reference to another string-array resource?
For instance, say I have an array like this
<string-array
android:name="names">
<item>Danny</item>
<item>Trevor></item>
<item>Garth</item>
</string-array>
is there some way to get the effect implied by this:
<string-array
android:name="appellations"
android:value="#string-array/names" / >
such that, say,
getStringArray(R.array.appellations)
returns the "names" array?

Android - String Array with switch statement

I am looking for a guide as to how i can implement switch that would get values from a string array. Basically, i have a spinner with all the items as stated in my array, so i want to implement a switch so that whenever i click on an item, it will trigger an event, for example, going to another activity.
<string-array name="location1">
<item>string a</item>
<item>string a</item>
<item>string b</item>
<item>string c</item>
<item>string d</item>
</string-array>
So, if i have a string array like this, how should I implement my switch statement?
If array is:
<string-array name="cars_array">
<item>Audi</item>
<item>Ferrari</item>
</string-array>
You can access them like this:
Resources res = getResources();
String[] cars = res.getStringArray(R.array.cars_array);
then you can access them individually as cars[0], cars[1] etc
If you want to access the same string resource as part of an array and individually as well, then you can do this:
<string name="audi">Audi</string>
<string name="ferrari">Ferrari</string>
<string-array name="cars_array">
<item>#string/audi</item>
<item>#string/ferrari</item>
</string-array>
Then you can simply say "#string/audi" to get it individually as well as you can use the array name "#string/cars" to use the string array as a whole.
I think this resource on Android Developers site might help you : String Resources
It will get you something like that :
Resources res = getResources();
String[] strArray = res.getStringArray(R.array.location1);

Android: how to make string-array to be a reference to another string-array?

I can make string to be a reference to another string just like that:
<resources>
<string name="my_string">My String</string>
<string name="my_alias">#string/my_string</string>
</resources>
And i tried to make string-array reference the same way:
<string-array name="my_string_array">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
<string-array name="my_string_array_alias">#array/my_string_array</string-array>
But this does not work, unfortunately. I tried to use this array and alias in ListPreference:
<ListPreference
android:key="my_key"
android:title="#string/my_title"
android:entries="#array/my_string_array_alias"
android:entryValues="#array/my_string_array"
android:dialogTitle="#string/my_dialog_title" />
and entries is just an empty array. Of course, i can make both entries and entryValues to take the same array, but i like it to be a different ones.
Found this on the Android dev site.
Note: Not all resources offer a mechanism by which you can create an alias to another resource. In particular, animation, menu, raw, and other unspecified resources in the xml/ directory do not offer this feature.
It's a little vague but I believe this means the framework doesn't support it.

Categories

Resources