Is it possible to refer to Array Resource Items directly in XML? - android

In code, it's easy to use the Resources class to get an instance of an XML typed array resource and traverse its items. My question: is it possible to reference array resource items in XML itself as shown below
<resource>
<array name="items">
<item>Item One</item>
<item>Item Two</item>
<item>Item Three</item>
</array>
<string name="itemThree">#array/items[2]</string>
</resource>
The format shown above does not work. Does anyone know if that is possible using a different format?

No, but I think the reverse works. Define your strings as string resources and refer to them as #string/... in the <item> elements.

I would just define itemThree in Java:
String itemThree = getResources().getStringArray(R.array.items)[2]
Ultimately, the XML gets inflated into Java objects, so it's not much difference IMO.

Related

Android TypedArray of raw ids

Is there a way to add raw resource ids as items in a TypedArray. For example, if there is a resource res/raw/Foo.ext for a certain binary Foo.ext, is it possible to define a file res/values/arrays.xml that looks like:
<resources>
<array name="Foo">
<item name="id">#raw/Foo</item>
</array>
</resources>
Android Studio does not appear to show autocompletion for #raw/ as it does for #drawable/ for example. If possible, how could the item be retrieved programmatically?

Set constant choices in Android Spinner from layout.xml

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>

Dynamically add values to resource string-array in Android

Is there a way to dynamically add values to an Android resource string-array?
E.g.:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="titles">
</string-array>
</resources>
Is there a way to dynamically add values to an Android resource string-array?
No, because resources are read-only at runtime.
Yes! you can create a static array of strings in Android but dynamically adding values to an Android resource is not yet possible.
It turns out that it’s easy to create and use a static array of strings in Android. Of course, you can do this in Java code, as I describe in my Java string array tutorial, but for Android, I’m talking about doing this in XML.
In short, this is how you define a static string array in an Android XML file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="my_books">
<item>Scala Cookbook</item>
<item>Play Framework Recipes</item>
<item>How I Sold My Business: A Personal Diary</item>
<item>A Survival Guide for New Consultants</item>
</string-array>
</resources>
Then inside an Activity, Fragment, or other Java class, you can create a string array in Java from that XML like this:
Resources res = getResources();
String[] myBooks = res.getStringArray(R.array.my_books);

Android Spinner Option Value Just like Select Tag

Is it Possible I can create a spinner with options and values.
<select name=test>
<option value="1">Baran</option>
<option value="2">Khan</option>
</select>
with the spinner XML:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="test">
<item Value="1">Baran</item>
<item value="2">Khan</item>
</string-array>
</resources>
How can I accomplish such target. As I need to pass Ids to the Server.
You have to manage two list and both are dynamic as you want.
Step to achieve :
Create two ArrayList<String>.Depend on your data type here i make as String Array.
Add value to ArrayList.
Create custom adapter and pass two list adapter in that and get value according that.
Add list to Spinner Adapter. Get the index or position of
the Spinner.
Follow same index to get value from second list value.
Send that value to server.
Task over
See Demo Example that will Guide you to make easy.
Enjoy !!!
Not the best but one approach would be to create another string array with the ids:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="test">
<item Value="1">Baran</item>
<item value="2">Khan</item>
</string-array>
<string-array name="testIDS">
<item>1</item>
<item>2</item>
</string-array>
</resources>
Now when item i is selected from array test you can get the id from item i in array testIDS.

how to NAME a string-array using a string resource

How can I do this?
<string-array name="#string/a_string_from_resources">
Is it possible to name a string-array using a string Resource?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="#string/a_string_from_resources">
<item>first</item>
<item>second</item>
<item>third</item>
<item>fourth</item>
<item>fifth</item>
</string-array>
</resources>
<string-array name="array_name">
My guess is you cant! Because the R.java is generated from all the resources at the same time. As the xmls are meaningless before generating, resource files wont be able to gather the resources from themselves.

Categories

Resources