Format an array of strings with arguments - android

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...)

Related

Can't get rupee symbol to show correctly from strings.xml

So this works fine:
strFoo = "\u20B9" + strBar
But this doesn't
strFoo = R.string.rupee_symbol.toString() + strBar //.toString() is required
//R.string.rupee_symbol.toString() evaluates to some random number 2131755148... which I believe is a character array...
strings.xml
<string name="rupee_symbol">\u20B9 </string>
I can't figure out why it would behave like that, it looks like the same thing...!
You should not concatenate strings with string resources instead, you can use place holder:
<string name="rupee_symbol">\u20B9%s</string>
And use:
strFoo = resources.getString(R.string.rupee_symbol, strBar)
use getString(R.string.rupee_symbol) instead R.string.rupee_symbol.toString()
For example-
String strBar = String.valueOf(100);
String strFoo = getString(R.string.rupee_symbol)+strBar;
textView.setText( strFoo);

how to set text to textfield android with one default value along with an additional value [duplicate]

I have strings defined in the usual strings.xml Resource file like this:
<string name="hello_world"> HELLO</string>
Is it possible to define format strings such as the one below
result_str = String.format("Amount: %.2f for %d days ", var1, var2);
in the strings.xml resource file?
I tried escaping the special characters but its not working.
You do not need to use formatted="false" in your XML. You just need to use fully qualified string format markers - %[POSITION]$[TYPE] (where [POSITION] is the attribute position and [TYPE] is the variable type), rather than the short versions, for example %s or %d.
Quote from Android Docs: String Formatting and Styling:
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
In this example, the format string has two arguments: %1$s is a
string and %2$d is a decimal integer. You can format the string with
arguments from your application like this:
Resources res = getResources();
String text = res.getString(R.string.welcome_messages, username, mailCount);
You should add formatted="false" to your string resource
Here is an example
In your strings.xml :
<string name="all" formatted="false">Amount: %.2f%n for %d days</string>
In your code:
yourTextView.setText(String.format(getString(R.string.all), 3.12, 2));
Inside file strings.xml define a String resource like this:
<string name="string_to_format">Amount: %1$f for %2$d days%3$s</string>
Inside your code (assume it inherits from Context) simply do the following:
String formattedString = getString(R.string.string_to_format, floatVar, decimalVar, stringVar);
(In comparison to the answer from LocalPCGuy or Giovanny Farto M. the String.format method is not needed.)
Quote from Android Docs:
If you need to format your strings using String.format(String,
Object...), then you can do so by putting your format arguments in the
string resource. For example, with the following resource:
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
In this example, the format string has two arguments: %1$s is a string
and %2$d is a decimal number. You can format the string with arguments
from your application like this:
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
For me it worked like that in Kotlin:
my string.xml
<string name="price" formatted="false">Price:U$ %.2f%n</string>
my class.kt
var formatPrice: CharSequence? = null
var unitPrice = 9990
formatPrice = String.format(context.getString(R.string.price), unitPrice/100.0)
Log.d("Double_CharSequence", "$formatPrice")
D/Double_CharSequence: Price :U$ 99,90
For an even better result, we can do so
<string name="price_to_string">Price:U$ %1$s</string>
var formatPrice: CharSequence? = null
var unitPrice = 199990
val numberFormat = (unitPrice/100.0).toString()
formatPrice = String.format(context.getString(R.string.price_to_string), formatValue(numberFormat))
fun formatValue(value: String) :String{
val mDecimalFormat = DecimalFormat("###,###,##0.00")
val s1 = value.toDouble()
return mDecimalFormat.format(s1)
}
Log.d("Double_CharSequence", "$formatPrice")
D/Double_CharSequence: Price :U$ 1.999,90

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

format statement in a string resource file

I have strings defined in the usual strings.xml Resource file like this:
<string name="hello_world"> HELLO</string>
Is it possible to define format strings such as the one below
result_str = String.format("Amount: %.2f for %d days ", var1, var2);
in the strings.xml resource file?
I tried escaping the special characters but its not working.
You do not need to use formatted="false" in your XML. You just need to use fully qualified string format markers - %[POSITION]$[TYPE] (where [POSITION] is the attribute position and [TYPE] is the variable type), rather than the short versions, for example %s or %d.
Quote from Android Docs: String Formatting and Styling:
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
In this example, the format string has two arguments: %1$s is a
string and %2$d is a decimal integer. You can format the string with
arguments from your application like this:
Resources res = getResources();
String text = res.getString(R.string.welcome_messages, username, mailCount);
You should add formatted="false" to your string resource
Here is an example
In your strings.xml :
<string name="all" formatted="false">Amount: %.2f%n for %d days</string>
In your code:
yourTextView.setText(String.format(getString(R.string.all), 3.12, 2));
Inside file strings.xml define a String resource like this:
<string name="string_to_format">Amount: %1$f for %2$d days%3$s</string>
Inside your code (assume it inherits from Context) simply do the following:
String formattedString = getString(R.string.string_to_format, floatVar, decimalVar, stringVar);
(In comparison to the answer from LocalPCGuy or Giovanny Farto M. the String.format method is not needed.)
Quote from Android Docs:
If you need to format your strings using String.format(String,
Object...), then you can do so by putting your format arguments in the
string resource. For example, with the following resource:
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
In this example, the format string has two arguments: %1$s is a string
and %2$d is a decimal number. You can format the string with arguments
from your application like this:
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
For me it worked like that in Kotlin:
my string.xml
<string name="price" formatted="false">Price:U$ %.2f%n</string>
my class.kt
var formatPrice: CharSequence? = null
var unitPrice = 9990
formatPrice = String.format(context.getString(R.string.price), unitPrice/100.0)
Log.d("Double_CharSequence", "$formatPrice")
D/Double_CharSequence: Price :U$ 99,90
For an even better result, we can do so
<string name="price_to_string">Price:U$ %1$s</string>
var formatPrice: CharSequence? = null
var unitPrice = 199990
val numberFormat = (unitPrice/100.0).toString()
formatPrice = String.format(context.getString(R.string.price_to_string), formatValue(numberFormat))
fun formatValue(value: String) :String{
val mDecimalFormat = DecimalFormat("###,###,##0.00")
val s1 = value.toDouble()
return mDecimalFormat.format(s1)
}
Log.d("Double_CharSequence", "$formatPrice")
D/Double_CharSequence: Price :U$ 1.999,90

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)

Categories

Resources