user210504
user210504

Reputation: 1759

Display unicode characters in TextView Android

There are a number of posts all over the internet on this topic, however none of them have been able to isolate and solve the problem.

I am trying to show some special UTF-8 encoded symbols stored in a SQLite database using a TextView, however all it shows is boxes. I understand what this means is that right font is not installed. But when I print those symbols using Arial font on Mac it works.

I am trying to use an Arial typeface on the device and the emulator.

Any advise.

Upvotes: 9

Views: 5601

Answers (2)

edovino
edovino

Reputation: 3325

It works on your Mac because the font used by your mac contains those special characters. It would be impossible to create a fontfile that contains all characters defined in unicode. Chinese fonts are a good example: none of them contain all of the ~60.000 known Chinese characters because the font file would be huge and unusable.

You could try using the font from your Mac on android (might be copyright issues - try finding a free font that contains the characters: package the (ttf) file in your application, for example in the /assets folder, and then in your application load that font with

TypeFace typeFace = Typeface.createFromAsset(assetmanager,"fontfile.ttf");

You can then use this in a TextView like so:

TextView view = (TextView) findById(R.id.mytext);

view.setTypeFace(typeFace);

Upvotes: 7

Dave MacLean
Dave MacLean

Reputation: 5173

If I'm understanding your situation correctly, you might want to consider the StringEscapeUtils.unescapeXml() method. It's in the common-lang.jar file available here: http://commons.apache.org/lang/

It will take encoded symbols (for example ' for an apostrophe) and get you to a proper character.

Upvotes: 3

Related Questions