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>
Related
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);
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
I got this string resource:
<string name="about_app_text"><b>%1$s</b> some text.\n\n
<b>%2$s, text</b>, more text.</string>
The text inside the <b> tag does not get bold. Why is that?
<string name="about_app_text"><![CDATA[<b>%1$s</b> some text.\n\n
<b>%2$s, text</b>, more text.]]></string>
then parse as Spannable
Spanned spanned = Html.fromHtml(getString(R.string.about_app_text));
textView.setText(Spanned);
you can try this
YourTextview.setText(Html.fromHtml(getResources().getString(R.string. about_app_text)));
string resource:
<string name="about_app_text"><![CDATA[<b>%1$s</b> some text.\n\n
<b>%2$s, text</b>, more text.]]></string>
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 have set text in text view
TextView text = view.FindViewById<TextView>(Resource.Id.details);
text.SetText(_text, TextView.BufferType.Normal);
it sets unknown characters when displays
how to write unicode characters correct ?
must be text like this
Try this,
text.setText(Html.fromHtml(_text));
Define your characters/string, which you want to display, in res/values/strings.xml. Load it in textview like
strings.xml
<string name="unicodechar">my unicode char is here</string>
in Java:
String _text = context.getResources().getString(R.string.unicodechar);
textview.settext(_text);
Try using a method like this to convert the string:
public String encodeUTF(String str) throws UnsupportedEncodingException {
byte[] utf8Bytes = str.getBytes("UTF-8");
String encodedStr = new String(utf8Bytes, "UTF-8");
return encodedStr;
}
Normally it will either need to be UTF-8 or ISO 8859-1
Use a font like DejaVuSans.ttf. Copy this in assets folder. Then set the typeface of the text view as below. After this try setting the text.
tv=(TextView)findViewById(R.id.textView1);
Typeface font= Typeface.createFromAsset(getAssets(), "DejaVuSans.ttf");
tv.setTypeface(font);