How getting an array of xml file from android resource? - android

I have this array into my resource file :
<array name="xml_data">
<item>#xml/data1</item>
<item>#xml/data2</item>
<item>#xml/data3</item>
<item>#xml/data4</item>
</array>
Normally, it's not different from an normal array, but when getting in code, this doesn't work...
final Resources res = getResources();
int[] xmlList = res.getIntArray(R.array.xml_data);
Log.i(TAG, "Data found: "+ xmlList.length);
for (int i = 0; i < xmlList.length; i++) {
Log.i(TAG, "Extract xml id="+ xmlList[i].);
}
Here is the output obtained in the logcat :
Data found: 4
Extract xml id=0
Extract xml id=0
Extract xml id=0
Extract xml id=0
Can you help me about this?
Thanks.

For your convenience lets take an example:
I had a simple 'string' array( not int) in a sample xml file. Lets call it array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="doing">
<item>1</item>
<item>2/item>
<item>3/item>
<item>3</item>
</string-array>
</resources>
Now in my Java file, I called it as follows inside my OnCreate() function and it worked:
String[] xmlList = getResources().getStringArray(R.array.doing);
int first = Integer.parseInt(xmlList[0]);
Log.d("test", "1st string is: " + first);
P.S: I haven't tested the code but I hope you got the logic. All the best. Hope it helps.

Use your TypedArray like this:
TypedArray mTypedArray = getResources().getTypedArray(R.array.xml_data);
Then, retrieve your resource ID's like this:
int id = mTypedArray.getResourceId(position, defaultResourceId);
When I used this, it was an array of String IDs, so I followed that with this:
getString(id)

Related

How I can get IntArray of view ids from resource XML (Android)

I have array in file res/values/ids.xml
<array name="first_set">
<item>#id/layout1</item>
<item>#id/layout2</item>
<item>#id/layout3</item>
...
</array>
I tried to get these ids, but it didn't work(((
val typedArray = resources.obtainTypedArray(viewSetId)
val idSet = IntArray(typedArray.length())
for (i in 0 until typedArray.length()) {
idSet[i] = typedArray.getInt(i, 0)
}
typedArray.recycle()
All idSet's elements equals 0.
I also tried store integer array in res/values/integers.xml
<integer-array name="first_set">
<item>#id/layout1</item>
<item>#id/layout2</item>
<item>#id/layout3</item>
...
</integer-array>
And then get these ids
val idSet = resources.getIntArray(R.array.first_set)
But result is the same((
IntArray Xml should be like this Format
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="first_set">
<item>#id/layout1</item>
<item>#id/layout2</item>
<item>#id/layout3</item>
</integer-array>
</resources>
in the path xml resource in res/values/integers.xml
Access the xml data in the activity like this way
val arrayValues = resources.getIntArray(R.array.first_set)
Use #+id to ensure they are created by the time the array is generated:
<integer-array name="first_set">
<item>#+id/layout1</item>
<item>#+id/layout2</item>
<item>#+id/layout3</item>
...
</integer-array>

Android getString issue

So I have many strings in strings.xml, they are recorded in a format of:
<string name="list_1">xxxxxxxxxxx</string>
<string name="list_2">xxxxxxxxxxx</string>
<string name="list_3">xxxxxxxxxxx</string>
......
Now I want to load them one by one without having to type all the string IDs. I want to load them in a fashion like:
for (int i = 1; i <= num; i++) {
// Just showing what I mean.
String xxx = getString(R.string.("list_" + i));
}
Is there a method to do so?
Try this:
int resourceID = getResources().getIdentifier("list_" + i, "string", getPackageName());
String xxx = getString(resourceID);
It would be worth to use resource arrays. For example
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
and you can access, iterate as follows:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

color-array from color.xml returns null for all items.

I have made an color-array containing 12 different colors in color.xml.
But in my attempt to extract the colors and use them in the code I get null for all values in the array. I also tried to use the TypedArray solution with no difference. So what is wrong?
public void testColor(){
Resources resources = App.getAppContext().getResources();
String colors[] = resources.getStringArray(R.array.backgroundcolors);
//prints null
Log.d("TAG", " " + colors[3]);
//prints 12x null
for(String x : colors){
Log.d("TAG", " " + x);
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testColor();
}
color.xml
<array name="backgroundcolors">
<item>#000000</item>
<item>#373737</item>
<item>#ffffff</item>
<item>#e6e6e6</item>
<item>#EAE1D8</item>
<item>#fd79a1</item>
<item>#ff0f68</item>
<item>#E849A1</item>
<item>#F7E84E</item>
<item>#FFB732</item>
<item>#48B1E3</item>
<item>#5dd95d</item>
</array>
change:
<array name="backgroundcolors">
to
<string-array name="backgroundcolors">
if you want to use getStringArray, you should be useing
<string-array
as root tag instead of <array and content should be placed in strings.xml. Colors are int. You can use getIntArray to retrive an array of int from the res
You can use
Color.parseColor(colors[i]))
Color.parseColor("#636161")
Solution 1
You should get the array as int array like this
int colors[] = resources.getIntArray(R.array.backgroundcolors);
Solution 2
If you want to read it as a String array then
change
<array name="backgroundcolors">
to
<string-array name="backgroundcolors">

Putting resource identifiers into xml Array

In the first case, how do I put the the R.string resource identifier number into an integer array.
In the second case, how do I instead put the number associated with the R.integer resource identifier into the array.
<integer-array
name="integer_array_name">
<item>#string/nav_heading</item>
<item>#integer/viewtype_heading</item>
....
</integer-array>
Using android studio if that makes any difference.
It appears one answer is to load the xml as a typed-array if you want to load just resource identifiers, and to load it as an integer-array if you want to load just resolved ints. You don't have to change the type of array in your xml, you can just leave it as a generic "array" as I've done in the tests
<array
name="testArray">
<item>#string/nav_vault_heading</item>
<item>#integer/viewtype_heading</item>
</array>
//Test 1
int[] intArray= getActivity().getResources().getIntArray(R.array.testArray);
Log.d("MyInts", "IntArray = " + Arrays.toString(intArray));
06-26 16:31:02.826 12952-12952/... D/MyInts﹕ IntArray = [0, 32]
//Test2
Due Credit: http://www.anddev.org/xml_integer_array_resource_references_getintarray-t9268.html
TypedArray typedArray= getActivity().getResources().obtainTypedArray(R.array.testArray);
for(int i=0; i<typedArray.length(); i++){
int id = typedArray.getResourceId(i, 0);
Log.d("MyInts", "MyInts: " + id);
}
06-26 16:31:02.826 12952-12952/... D/MyInts﹕ MyInts: 2131165274
06-26 16:31:02.826 12952-12952/... D/MyInts﹕ MyInts: 2131427334

Format an array of strings with arguments

I'd like to format an array of strings just like android used to format strings:
Usually we do:
strings.xml
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
In some java code:
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
I'm looking for something like:
in some arbitrary xml:
<string-array name="employee">
<item>name: %1$s</item>
<item>post: %2$s</item>
</string-array>
in some java code:
Resources res = getResources();
String[] employee = ArrayString.format(res.getStringArray(R.string.employee), name, post);
Is there an elegant way to do that?
EDIT:
The next pieces of code is a workaround and I'm posting it just to help #Sufian, who asked for it in a comment. It's not a real answer once my question is about format the string array's content and the bellow code is formatting each string separately.
In some misc.xml:
<string-array
name="string_array">
<item>1st position: %1$d</item>
<item>2nd position: %1$d</item>
</string-array>
Then, in java code:
res = getResources();
String[] sa = res.getStringArray(R.array.string_array);
for (int i = 0; i < sa.length; i++ ) {
text += String.format(sa[i], i);
}
Just use:
String text = String.format(res.getStringArray(R.array.myStringArray)[index], param1, param2);
getQuantityString may solve your problem.
Look at quantity strings in http://developer.android.com/guide/topics/resources/string-resource.html
Here's the specific API doc:
http://developer.android.com/reference/android/content/res/Resources.html#getQuantityString(int,%20int,%20java.lang.Object...)

Categories

Resources