How can I display special characters(like – "
) in a TextView?
If you know the Unicode value, you can display any UTF-8 character. Example, for " you would have &\#0034;
.
See Unicode Characters (at Code Table) for more information.
I've written a custom method that will convert all unicode from hexa to integer and replaces from actual string. So that text view can read is as a unicode. have a look ,this will solve your problem...
public String unecodeStr(String escapedString) {
try {
String str;
int from = 0;
int index = escapedString.indexOf("\\u", 0);
while (index > -1) {
str = escapedString.substring(index, index + 6).replace("\\u", "");
try {
Integer iI = Integer.parseInt(str, 16);
char[] chaCha = Character.toChars(iI);
escapedString = escapedString.replaceFirst(str, String.valueOf(chaCha));
} catch (Exception e) {
CustomLog.e("error:", e.getMessage());
}
from = index + 3;
index = escapedString.indexOf("\\u", from);
}
escapedString = escapedString.replace("\\u", "");
} catch (Exception e) {
CustomLog.info("warnning", "emoji parsing error at " + escapedString);
}
return escapedString;
}
You can use the Html.fromHtml()
to handle HTML formatted text into a Spannable
that TextView
can display.
I've implemented this solution.
Activity class:
textView.setText( getString(R.string.author_quote, "To be or not to be", "Shakespeare") )
strings.xml:
<string name="author_quote">« %1$s » - %2$s</string>
HTML chars are written directly in strings.xml, no additional Html.fromHtml() is needed. It works fine on all my devices.