I have the following XML
<string-array name="str_arr1">
<item>My item 1</item>
<item>My item 2</item>
<item>My item 3</item>
</string-array>
<string-array name="str_arr2">
<item>My item 4</item>
<item>My item 5</item>
<item>My item 6</item>
</string-array>
How can I reference the above strings arrays using an array.. something like below (maybe another type of array needs to be used?)
<string-array name="my_strings_arrays">
<item>R.array.str_arr1</item>
<item>R.array.str_arr2</item>
</string-array>
Basically in the code, I want to read the my_strings_arrays and then for each array , I want to grab the list of items within.
In any xml resource file we can use reference of array/string/integer/color/etc. with "#" prefix.
So, here we can use #array for getting reference of another array str_arr1 and str_arr2.
<string-array name="str_arr1">
<item>My item 1</item>
<item>My item 2</item>
<item>My item 3</item>
</string-array>
<string-array name="str_arr2">
<item>My item 4</item>
<item>My item 5</item>
<item>My item 6</item>
</string-array>
You to need change your code as below.
<string-array name="my_strings_arrays">
<item>#array/str_arr1</item>
<item>#array/str_arr2</item>
</string-array>
Your second array should be an array of string arrays, not string array of string arrays.
I think the way you would do what you need is:
<array name="my_strings_arrays">
<item>#array/str_arr1 </item>
<item>#array/str_arr2 </item>
</array>
Related
I have a spinner in my layout that I would like to populate with pre-determined, hard-coded data.
Of course I can dynamically add the data from my Activity class, but I want to know if there is a way to do this in the xml itself.
All of the choices that I want to appear in the spinner are present in arrays.xml. Is there a way to plug in this data into the Spinner?
plug this into your spinner assuming that you have properly created you xml array...
android:drawSelectorOnTop="true"
android:entries="#array/array_name"
Your String Resources you should add the array like so...
<string-array name="array_name">
<item>Array Item One</item>
<item>Array Item Two</item>
<item>Array Item Three</item>
</string-array>
This worked for me with a string-array named “count” loaded from the projects resources:
Spinner spinnerCount = (Spinner)findViewById(R.id.spinner_counts);
ArrayAdapter<String> spinnerCountArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.count));
spinnerCount.setAdapter(spinnerCountArrayAdapter);
let me know if it will fulfill your requirements.
This is my resource file (res/values/arrays.xml) with the string array:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name=“count”>
<item>0</item>
<item>5</item>
<item>10</item>
<item>100</item>
<item>1000</item>
<item>10000</item>
</string-array>
</resources>
Is something like this possible??
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item value=My >Mercury</item>
<item value=Vs >Venus</item>
<item value=EA >Earth</item>
<item value=Ms >Mars</item>
</string-array>
</resources>
the listView will show the planets but when the user selects Mars
the value I get will be Ms.
No, there is no such direct way to use string array in android app resources. One of my suggestion is separate the label and value to two different string arrays, then use these string arrays in your code.
<string-array name="planet_label">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
<string-array name="planet_values">
<item>My</item>
<item>Vs</item>
<item>Ea</item>
<item>Ms</item>
</string-array>
I have a navigation drawer menu with diferent items in a listview. This item´s names are in arrays.xml like this one in values-es and other one in values:
<string-array name="nav_drawer_items">
<item>One</item>
<item>Two</item>
<item>Three</item>
<item>Four</item>
</string-array>
I have done a method to translate the app but this items not change when a different language is selected. The others textview work perfect. What I am doing wrong? it is possible to translate an array item?
Try this, but do not forget initially define variables.
<string-array name="nav_drawer_items">
<item>#string/one</item>
<item>#string/two</item>
<item>#string/three</item>
</string-array>
I have this following spinner in my main activity
My problem is that the spinner's value is required when submitting but i want its default value to be empty so i put in my arrays.xml an item with empty value. so when the app is lunched it will show a empty spinner.
The problem is that when they click the spinner and the drop down display the first selection is empty.
my solution is to put the first data as "Select" but is there a way that the value would be empty like in html select tag?
<option value="">Select</option>
<option value="data 1">data 1</option>
on android xml is this possible? or how to do it? i tried this but still not working. it passes the value "Select"
<item value="">Select</item>
<item value="data 1">data 1</item>
my spinner
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/spinner_arr"
android:tag="relation" />
my arrays.xml
<string-array name="spinner_arr">
<item></item>
<item>data 1</item>
<item>data 2</item>
<item>data 3</item>
<item>data 4</item>
<item>data 5</item>
</string-array>
Try using the prompt attr of Spinner, if that's not work you'll have to craete some adapter of your own.
take a look here:
How to make an Android Spinner with initial text "Select One"
I have string array like this:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<string-array name="my_videos">
<item>bunny.mp4</item>
<item>bunny.m4a</item>
<item>bunny.3gp</item>
</string-array>
And in my app I have this:
String product = ((TextView) view).getText().toString();
Now I want to put some normal text like this:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<string-array name="my_videos">
<item name="bunny.mp4">Movie description 1</item>
<item name="bunny.m4a">Movie description 2</item>
<item name="bunny.3gp">Movie description 3</item>
</string-array>
Now I want to acces name attribute and not get text.
I am new to Android and do not know if this is possible.
Please help.