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>
Related
I have declared following declare-stylables in attr.xml:
<declare-styleable name="SideSpinnerAttrs">
<attr name="stringValues" format="reference" />
<attr name="iconIDs" format="reference"/>
</declare-styleable>
Array of resource icons in array.xml:
<integer-array name="spinnerIcons">
<item>#drawable/ic_attachment_black_24dp</item>
<item>#drawable/ic_audiotrack_black_24dp</item>
<item>#drawable/ic_slideshow_black_24dp</item>
</integer-array>
I would like to call and set those icons from array to an imageView:
private void readSpinnerIcons(Context context, AttributeSet attrs) {
TypedArray icons=context.obtainStyledAttributes(attrs,R.styleable.SideSpinnerAttrs);
int id=icons.getResourceId(R.styleable.SideSpinnerAttrs_iconIDs,0);
int[] i=getResources().getIntArray(id);
spinner_icon.setBackgroundResource(i[0]);
}
But, array "int[] i", is empty. Why?
For example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="IntArray">
<item>2</item>
<item>8</item>
<item>10</item>
<item>16</item>
</integer-array>
</resources>
You can use this
Resources r = getResources();
int[] bases = r.getIntArray(R.array.IntArray);
The problem is this line:
int id=icons.getResourceId(R.styleable.SideSpinnerAttrs_iconIDs,0);
The first argument is not the Styleable resource id, but rather the index of the TypedArray containing the resource id. Since you're not providing a valid index, id will always be the default value you're using as the 2nd argument, which means your int[] array i will always be empty.
Also, make sure you always call recycle() once you're done using a TypedArray. Use the following:
private void readSpinnerIcons(Context context) {
TypedArray icons = context.obtainStyledAttributes(new int[] {R.styleable.SideSpinnerAttrs});
int id = icons.getResourceId(0, 0);
int[] i = getResources().getIntArray(id);
spinner_icon.setBackgroundResource(i[0]);
icons.recycle();
}
I have this file array.xml and I want to get the item value from an array.
How can I do that? I have tried with getInt but that returns 0. All help is welcome.
<resources>
<array name="firstAd">
<item>border_top_id_1v</item>
<item>R.id.dugme_1v</item>
<item>R.id.rent_or_buy_1v</item>
<item>R.id.currency_1v</item>
<item>R.id.price_1v</item>
<item>R.id.name_1v</item>
<item>R.id.address_1v</item>
</array>
</resources>
First, change each item to #id/object instead of R.id.object, then change the tag from array to integer-array, and move the code to your 'integer.xml' resource file.
integer.xml:
<resources>
<integer-array name="firstAd">
<item>#id/dugme_1v</item>
<item>#id/rent_or_buy_1v</item>
<item>#id/currency_1v</item>
<item>#id/price_1v</item>
<item>#id/name_1v</item>
<item>#id/address_1v</item>
</integer-array>
</resources>
Then, programmatically use a TypedArray, like so:
TypedArray firstAd = getResources().obtainTypedArray(R.array.firstAd);
int resourceId = firstAd.getResourceId(index, defValue);
try #id/yourIDHere
<item>border_top_id_1v</item>
<item>#id/dugme/1v</item>
<item>#id/rent_or_buy_1v</item>
<item>#id/currency_1v</item>
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">
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)
Good day, Any idea why i could be getting a NullPointerException in the following code. I am trying to retrieve elements from a string-array in XML and test against a given value but i keep getting a NullpointerException on the first line of the code below.
String [] position = context.getResources().getStringArray(R.array.google_weather_conditions); // NullpointerException here
for(String check: position){
String test_condition = check;
if(this.condition.equals(test_condition)){
weather_conditions = test_condition;
}
}
here is the strings.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="Clear">Clear</string>
<string name="Sunny">Sunny</string>
<string name="Mostly_Sunny">Mostly Sunny</string>
<string name="Partly_Sunny">Partly Sunny</string>
<string name="Cloudy">Cloudy</string>
<string name="Mostly_Cloudy">Mostly Cloudy</string>
<string name="Partly_Cloudy">Partly Cloudy</string>
<string name="Rain">Rain</string>
<string-array name="google_weather_conditions">
<item>#string/Clear</item>
<item>#string/Sunny</item>
<item>#string/Mostly_Sunny</item>
<item>#string/Partly_Sunny</item>
<item>#string/Cloudy</item>
<item>#string/Mostly_Cloudy</item>
<item>#string/Partly_Cloudy</item>
<item>#string/Rain</item>
</string-array>
</resources>
Most likely context is null at the point of the NPE.
context.getResources().getStringArray(R.array.google_weather_conditions); // NullpointerException here
The following could be null:
context
return value of context.getResources()
R
R.array
R.array.google_weather_conditons
Time to either step through with a debugger, or break it down, line by line and see where it occurs.
What is R?