Get resource id from typedarray - android

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>

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>

How to get integer resource array through typed-array?

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();
}

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">

How getting an array of xml file from android resource?

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)

How to get a string from attr with reference format type?

I have my custom attr.xml document in which I specified declare-styleable:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="EditTextValidateablePreference">
<attr name="errorMessage" format="reference" />
</declare-styleable>
</resources>
Then in layout I set:
<com.xx.yy.EditTextValidateablePreference
...
ns:errorMessage="#string/validation_email_mail_server_invalid"
/>
And in EditTextValidateablePreference.class I get it with:
String validatorErrorMessage = attrs.getAttributeValue(PREFERENCE_NS, "errorMessage");
validatorErrorMessage has a value like: #2131099700
How can I get its integer value to use it with:
context.getResources().getString(messageId)
?
Thanks !
There's an excellent general answer which I recommend you to refer to: Declaring a custom android UI element using XML
In particular, you should use Context.obtainStyledAttributes(AttributeSet set, int[] attrs) and TypedArray.getString(int index) instead of AttributeSet.getAttributeValue(...):
TypedArray ta = activityContext.obtainStyledAttributes(attrs, R.styleable.EditTextValidateablePreference);
String theString = ta.getString(R.styleable.EditTextValidateablePreference_errorMessage);
ta.recycle();
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.EditTextValidateablePreference);
int resID = array.getResourceId(R.styleable.EditTextValidateablePreference_errorMessage, R.string.default_text);
And from this int, you can get the string by saying...
getResources().getString(resID);

Categories

Resources