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.
Related
I have an array of strings (in strings.xml), in which I collect all my games
<string-array name="games">
<item name="select" translatable="false">...</item>
<item name="game_fortnite" translatable="false">Fortnite</item>
<item name="game_csgo" translatable="false">CounterStrike: Global Offensive</item>
<item name="game_minecraft_minigames" translatable="false">Minecraft Minigames</item>
</string-array>
I am now trying to get a specific item (In this case the first one, but I will need others later) from this array. Since i can give the items names, without Android Studio underlining it for me, I thought maybe i can refer to the strings by names, which does not work in any way I tried it.
In this case I am trying to find out wheter my string variable "game" has the same value as the array item I have named "select". I have tried all of the following:
if(game.equals(R.array.games.select)){
}
if(game.equals(R.string.games.select)){
}
if(game.equals(R.array.games[0])){
}
if(game.equals(R.array.select)){
}
I heve tried, as you can see, using an index (didn't work), but I'd like a possibility to refer to them by their name property.
This will reach your xml string and then you can use java string,
String gamesArr[] = getResources().getStringArray(R.array.games);
After doing this, create a list view and fill the list view items with that array.
And below code will give you selected item on the list view.
myList.getSelectedItem();
As per android documentation, the item tag for string-array does not have any attribute available.
https://developer.android.com/guide/topics/resources/string-resource#StringArray
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.
I am thinking of creating a small dynamic layout using strings.xml as a base.
I know how to select single resources but is there a way of selecting ALL resources with a name like "Intro_"
A bit like a Select where like query.
So if i had some strings like;
Intro_Welcome
Intro_FirstParagraph
SecondScreen_Welcome
List of strings mList = select all string resources with a name like "Intro_"
mList would now contain the string of Intro_Welcome and Intro_FirstParagraph
Any help is usefull
Thank you
You can't do this like you described. The string resources name that you define in your xml file are mapped into int values. So, when you use they in your code, you use by the int value created.
You may consider using a modified version of this class, that can get the string resource id within the string text.
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));
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