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()
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 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">
If I have this string array:
<string-array name="htmlstrings">
<item><i>blah<br />blah</i></item>
<item><b>1st line<br />2nd line</b></item>
</string-array>
And I get it and set it as text:
String[] htmlstrings = getResources().getStringArray(R.array.htmlstrings);
textV.setText(Html.fromHtml(htmlstrings[1]));
As output I get:
1st line2nd line
But I want to get
1st line
2nd line
For regular strings (not string-array) I know I can get and display HTML with getText() like:
textV.setText(getText(R.string.onehtmlstring));
but I don't know what's the getText() equivalent for a string-array.
Use getTextArray() to make a CharSequence array, and set your TextView using that array.
CharSequence[] htmlchars = getResources().getTextArray(R.array.htmlstrings);
textV.setText(htmlchars[1]);
Have you try below code:-
Spanned sp = Html.fromHtml( getString(R.string.htmlsource));
tv.setText(sp);
or
Set TextView text from html-formatted string resource in XML
Try with CDATA attribute:
<string-array name="channel_link">
<item><![CDATA[https://news.google.com/news/feeds?pz=1&cf=all&ned=in&hl=en&output=rss]]></item>
</string-array>
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...)
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)