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
Related
In order to build vibrate pattern, I tried to define the array of data, in XML.
<string-array name="tab_bip_1">
<item>0</item>
<item>100</item>
<item>100</item>
</string-array>
Resources r = getResources();
long[]tab_vibrate = r.getIntArray(R.array.tab_bip_1); // Not correct...
Vib.vibrate(tab_vibrate, 0);
But to get array of values, it seems to be only possible to use getIntArray() which give an Int array, when vibrate needs a long array.
Is there a kind of "getLongArray()" method?
Or do I have to get data in string then loop to perform a long.parseLong() against each value?
Thanks
<string-array name="tab_bip_1">
<item>0</item>
<item>100</item>
<item>100</item>
</string-array>
You're trying to get IntArray, but this is a string array. Try this:
<integer-array name="tab_bip_1">
<item>0</item>
<item>100</item>
<item>100</item>
</integer-array>
And replace long with int, because it is just integer array.
Other than string resource we have in Android, we have the followings: https://developer.android.com/guide/topics/resources/more-resources.html
As you can see, there is not long type of xml resource. You need to use integer for number-type resources.
Thanks for the answers. Using an int array may be sufficient in this case but at the end I need to convert values as Vibrate don't want an Int Array but a Long one. :(
Also, using and Int array in XML is OK only if you have value lower than 32 bits. Not sure of what getIntArray() will say if we have an XML file with values greater than 32 bits.
I resolved my problem looping and casting string to long like this:
<string-array name="tab_bip_1">
<item>0</item>
<item>1000</item>
<item>2000</item>
</string-array>
// Get vibration array, which is in String
Resources r = getResources();
String[]str_tab_vibrate = r.getStringArray(R.array.tab_bip_1);
// Then loop around to transforme in long values
long[] long_tab_vibrate = new long[str_tab_vibrate.length];
for (int i = 0; i < str_tab_vibrate.length; i++) {
long_tab_vibrate[i] = Long.parseLong(str_tab_vibrate[i]);
}
// Then, vibrate
Vibrator Vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
Vib.vibrate(long_tab_vibrate, 0);
It works. Hope this will help.
I have a preference xml file and a listpreference.
The listpreference entryValues and entries are in a array.xml file.
Here's the problem, the entries/entryValues contain "10 mb/s" and i would like to get the int value from that entry/whatever is selected.
This gives me an error however, here is the code:
TextView t = (TextView)findViewById(R.id.textView1);
String result = sp.getString("BITRATE", "8");
int i = Integer.parseInt(result.substring(result.lastIndexOf(" mb/s")));
t.setText("Int value: " + i);
As i said this gives me an error and i cannot find the issue.
Thanks for any help!
FINAL FIXED CODE
TextView t = (TextView)findViewById(R.id.textView1);
String result = sp.getString("BITRATE", "3");
int intRes = Integer.parseInt(result);
t.setText("Int value: " + intRes);
Also i changed the preferences entryValues items to only be integers instead of ex 10mb/s to just 10.
Make your entryValues array containing only the integers. The entries are used when displaying to the user, the entryValues are what is stored in your preferences (and are not displayed). The two arrays do not have to have the same contents, but they should have the same number of items.
<string-array name="bitrate_entries">
<item>10 mb/s</item>
<item>25 mb/s</item>
<item>50 mb/s</item>
</string-array>
<string-array name="bitrate_entry_values">
<item>10</item>
<item>25</item>
<item>50</item>
</string-array>
Now you only need to use Integer.parseInt()
I try to change the background of my Activity ( onCreate ) randomly. It stucks in the the last step ... show it. Maybe someone has an idea for me.
I created an array within a xml file which contains 5+ drawable's - it looks like this ..
<array name="backgrounds">
<item>#drawable/bg1_320x480</item>
<item>#drawable/bg2_320x480</item>
<item>#drawable/bg3_320x480</item>
<item>#drawable/bg4_320x480</item>
<item>#drawable/bg5_320x480</item>
<item>#drawable/bg6_320x480</item>
</array>
Within my main activity, I get one random element out of the array ...
String[] mTempArray = getResources().getStringArray(R.array.backgrounds);
int iMin = 0;
int iMax = 5;
int randomIndex = iMin + (int) (Math.random() * iMax);
String resPath = mTempArray[randomIndex];
resPath return me (e.g.) res/drawable-hdpi/bg4_320x480.png. From this point on, I found a lot of solutions, but nothing brings me to succes.
What is the last point to set / change / overwrite the background?
A resource ID is just an integer--there's no need to deal with strings. You can use something like this
int[] imageIds = new int[] { R.drawable.bg1, R.drawable.bg2, ... };
, pick a random element, and set it as your background. Not sure if you can encode the array of resource IDs in xml.
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)
An XML file containing color names and hex codes is readily available to android programmers, such as:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="White">#FFFFFF</color>
<color name="Ivory">#FFFFF0</color>
...
<color name="DarkBlue">#00008B</color>
<color name="Navy">#000080</color>
<color name="Black">#000000</color>
</resources>
I can access a specific color using syntax such as:
TextView area1 = (TextView) findViewById(R.id.area);
area1.setBackgroundColor(Color.parseColor(getString(R.color.Navy)));
or
area1.setBackgroundColor(Color.parseColor("Navy"));
or
Resources res = getResources();
int rcol = res.getColor(R.color.Navy);
area1.setBackgroundColor(rcol);
How can I read in the entire xml file of colors into a String[] of color names AND an int[] of color resources (e.g., R.color.Navy), without having to specify each color name or resource ID?
Using the reflection API it's fairly simple (i had a similar problem with drawable-ids not a long time ago), but a lots of more experienced users said, that "Reflection on dalvik is really slow" so BE WARNED!
//Get all the declared fields (data-members):
Field [] fields = R.color.class.getDeclaredFields();
//Create arrays for color names and values
String [] names = new String[fields.length];
int [] colors = new int [fields.length];
//iterate on the fields array, and get the needed values:
try {
for(int i=0; i<fields.length; i++) {
names [i] = fields[i].getName();
colors [i] = fields[i].getInt(null);
}
} catch (Exception ex) {
/* handle exception if you want to */
}
Then if you have those arrays, then you can create a Map from them for easier access:
Map<String, Integer> colors = new HashMap<String, Integer>();
for(int i=0; i<hexColors.length; i++) {
colors.put(colorNames[i], hexColors[i]);
}
I think you will have to move your color.xml file into the /asset directory. You will be obliged to parse the XML "by hand", and it won't be possible to use the R.color.* syntax. (unless you choose to duplicate the file)
You could use introspection on R.colors to find out all field names and the associated values.
R.colors.getClass().getFields() will give you the list of all colors.
Using getName()on each field will provide you with the list of all color names and getInt() will give you the value of each color .