How to reference a string array in XML of Android layout file - android

The closest I could find on S.O. is this but it doesn't answer it.
It specifies here how to make a resource reference for a string, i.e. #string/string_name but further down under String Array in the documentation, it gives no option under Resource Reference for referencing the string array in XML like it does further up for String.

It's unclear what element you are trying to load the array into...
ListView has android:entries that accepts #array/ resource.
However, I find that adding adapters in the code makes more sense.
ArrayAdapter.createFromResource allows you to use an a String Array XML resource to load either a Spinner or a ListView using R.array in Java code.

Related

What means R.Layout ? R.?

i dont understand what is meant by R.layout or R.array in the following code, who describes
how to fill a spinner with drop-down menue with a array who has different countries as its elements:
adapter = ArrayAdapter.createFromResource(this, R.array.countries, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinnerCountry.setAdapter(adapter);
The string Array with the name "countries" is loacated in the strings.xml file, in folder res -> values. Why is it necessary to use R.array to locate the country array and why can i not use findViewbyID?
And second question is why is in R.layout the spinner called support_simple_spinner_dropdown_item?
If i choose spinner on the Design tab via drag and drop theres only one spinner to choose.
Would be nice to have some help. Thank you.
I looked on the Developersite and searched the forum.
First, the values under R are generated at compile time. They're simply a list of int values with names likes R.array.<name> or R.id.<name> that can be used as identifiers for actual arrays, views, or whatever.
I think the official docs explain what is happening quite well, but I'll explain in my own words anyway.
Why is it necessary to use R.array to locate the country array and why can i not use findViewbyID?
A string array is not a View, so it wouldn't make sense to use findViewById. findViewById is a method that returns the View referenced by an int value defined in the R.id constants.
R.array.countries will just be an int value that references a string array. To get the String[] for that id, you would instead use Resources::getStringArray(...). So, in the context of an Activity, you'd do final String[] array = getResources().getStringArray(R.array.countries);.
why is in R.layout the spinner called support_simple_spinner_dropdown_item
support_simple_spinner_dropdown_item is the id for a layout. You should understand some of the different components of a spinner. There is:
The View itself. There is only one of these, and it's what you see in the drag-and-drop option in the design/laout editor.
The layout id for each row in the spinner when it is open. This is defined in the layout xml file referenced by R.layout.simple_spinner_item.
The data used to populate each row. This is defined in the array referenced by R.array.countries.

How to add R.drawable resources to an array for android?

I would like to have sequence of set R.drawable been called in integer array so that it can be accessed later with choice.But I'll like to load to the R.drawable dynamically which corresponds to different names.It names needs to be had as per the external value input not hardcoded.I tried this in making many but like use list,set,array conversion etc.Kindly guide me with a snippet or example on this regard.Thank you.
I suppose, that best way to achieve this funcionality is by using ArrayAdapter of Integer.
You will simply add drawble resources by id.
If you need to get resource id, but you will get string input from user, you can use this code:
Android, getting resource ID from string?

Storing strings in android XML resource file and referencing them

I'm wondering if I should use a resource XML file for to store all the possible recipes for me rather than using an ArrayList that is hardcoded, the problem is that I don't know how to call from a resource file using the method i have...
Here is a cut down version of what I want:
int recipeNumber = b.getInt("RECIPE"); //This is taken from another activity
final TextView rowTextView = new TextView(this); //Create a textview
rowTextView.setText(R.string.recipeNumber); //This is what i am struggling with
howToLinearLayout.addView(rowTextView); //Add textview to linearlayout
I don't know what to put in the part that references my resource file. I know I need:
rowTextView.setText(R. but I'm not sure what would come after that.
Im storing strings that would be for example:
<string name="1">One part Vodka, One part Coke</string>
<string name="2">One part Vodka, One part Lemonade</string>
This list will be quite long so id appreciate any other suggestions on storage, bare in mind I'm pretty new to this.
The recipeNumber int is what string will be called in to the textview.
Thanks for any help
I recommend storing the values outside your code, for example in XML or JSON (quicker and easier to work with). I would go with JSON. Really easy to load a list into an array, or other collection, and work with it anyway you want.
http://www.vogella.com/articles/AndroidJSON/article.html
You can then maintain your recipes, add new ones, maintain it etc without having to touch the code. Simply replace the XML or JSON (or whatever you choose) and rebuild your app.
Strings.xml is intended for pieces of text in your UI and code which you might want to localise and to avoid hard coding strings. It's not really intended for storing data.
To get string resources in code, use getResources().
rowTextView.setText(getResources().getString(R.string.recipeNumber));
To get an unknown resource identifier, but a known resource name, you can use the following method.
int identifier = getResources().getIdentifier("" + recipeNumber, "string", "com.your.package.name");
rowTextView.setText(getResources().getString(identifier));

android linking string_array to textview

I have a strings.xml with a string-array resource. In another .xml I have a textview. setText() doesnt accept an array so how do I make my string array appear in the textview? I've done a search but can't seem to find something which addresses this issue. It is such a simple matter that I think I must be missing the obvious.
Ron
You can not refer to String Array resource in XML:
http://developer.android.com/guide/topics/resources/string-resource.html#StringArray
Check RESOURCE REFERENCE for both String and String Array resources. You can refer to String resource in XML, but you can not refer to String Array and Plurals resources.

Referencing a string in a string array resource with xml

I have preferences where you can enable/disable what items will show up on the menu. There are 17 items. I made a string array in values/arrays.xml with titles for each of these 17 items.
I have preferences.xml which has the layout for my preferences file, and I would like to reference a single item from the string array to use as the title.
How can I do this?
In the Android developer reference, I see how I can reference a single string with XML, but not how I can reference a string from an array resource in XML.
In short: I don't think you can, but there seems to be a workaround:.
If you take a look into the Android Resource here:
http://developer.android.com/guide/topics/resources/string-resource.html
You see than under the array section (string array, at least), the "RESOURCE REFERENCE" (as you get from an XML) does not specify a way to address the individual items. You can even try in your XML to use "#array/yourarrayhere". I know that in design time you will get the first item. But that is of no practical use if you want to use, let's say... the second, of course.
HOWEVER, there is a trick you can do. See here:
Referencing an XML string in an XML Array (Android)
You can "cheat" (not really) the array definition by addressing independent strings INSIDE the definition of the array. For example, in your strings.xml:
<string name="earth">Earth</string>
<string name="moon">Moon</string>
<string-array name="system">
<item>#string/earth</item>
<item>#string/moon</item>
</string-array>
By using this, you can use "#string/earth" and "#string/moon" normally in your "android:text" and "android:title" XML fields, and yet you won't lose the ability to use the array definition for whatever purposes you intended in the first place.
Seems to work here on my Eclipse. Why don't you try and tell us if it works? :-)
Maybe this would help:
String[] some_array = getResources().getStringArray(R.array.your_string_array)
So you get the array-list as a String[] and then choose any i, some_array[i].
The better option would be to just use the resource returned array as an array,
meaning:
getResources().getStringArray(R.array.your_array)[position]
This is a shortcut approach of other mentioned approaches but does the work in the fashion you want. Otherwise Android doesn't provide direct XML indexing for XML based arrays.
Unfortunately:
It seems you can not reference a single item from an array in values/arrays.xml with XML. Of course you can in Java, but not XML. There's no information on doing so in the Android developer reference, and I could not find any anywhere else.
It seems you can't use an array as a key in the preferences layout. Each key has to be a single value with it's own key name.
What I want to accomplish:
I want to be able to loop through the 17 preferences, check if the item is checked, and if it is, load the string from the string array for that preference name.
Here's the code I was hoping would complete this task:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
ArrayAdapter<String> itemsArrayList = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1);
String[] itemNames = getResources().getStringArray(R.array.itemNames_array);
for (int i = 0; i < 16; i++) {
if (prefs.getBoolean("itemKey[i]", true)) {
itemsArrayList.add(itemNames[i]);
}
}
What I did:
I set a single string for each of the items, and referenced the single strings in the . I use the single string reference for the preferences layout checkbox titles, and the array for my loop.
To loop through the preferences, I just named the keys like key1, key2, key3, etc. Since you reference a key with a string, you have the option to "build" the key name at runtime.
Here's the new code:
for (int i = 0; i < 16; i++) {
if (prefs.getBoolean("itemKey" + String.valueOf(i), true)) {
itemsArrayList.add(itemNames[i]);
}
}
Another way of doing it is defining a resources array in strings.xml like below.
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE resources [
<!ENTITY supportDefaultSelection "Choose your issue">
<!ENTITY issueOption1 "Support">
<!ENTITY issueOption2 "Feedback">
<!ENTITY issueOption3 "Help">
]>
and then defining a string array using the above resources
<string-array name="support_issues_array">
<item>&supportDefaultSelection;</item>
<item>&issueOption1;</item>
<item>&issueOption2;</item>
<item>&issueOption3;</item>
</string-array>
You could refer the same string into other xmls too keeping DRY intact.
The advantage I see is, with a single value change it would effect all the references in the code.
The answer is quite easy to implement.
String[] arrayName = getResources().getStringArray(R.array.your_string_array);
and now you can access any element of the array by index (let suppose i'th index), then you can access it by arrayName[i]
I hope you understand this

Categories

Resources