How can I display special characters (e.g. & ndash;) in a TextView?

How can I display special characters (e.g. – " ) in a TextView?

+6
source share
4 answers

You can use Html.fromHtml() to process HTML text in Spannable , which TextView can display.

+14
source

If you know the Unicode value, you can display any UTF-8 character. Example, for "you will have &\#0034;

See Unicode Characters (in the Code Table ) for more information.

+6
source

I implemented this solution.

Action class:

 textView.setText( getString(R.string.author_quote, "To be or not to be", "Shakespeare") ) 

strings.xml:

 <string name="author_quote">&#171; %1$s &#187; - %2$s</string> 

HTML characters are written directly in the strings.xml file, no additional Html.fromHtml () is required. It works great on all my devices.

+2
source

I wrote my own method that converts all unicode from hexa to integer and replaces the actual string. So the text representation can be read as unicode. 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; } 
0
source

Source: https://habr.com/ru/post/909117/


All Articles