4

When displaying japanese text in a textview, android defaults to using a chinese font, showing the wrong characters, exemplified here. Setting the locale to japanese works on the emulator, but doesn't work on my galaxy s3, probably because it doesn't support Japanese. There other solution is to set the font programattically from an asset such as:

textView.setTypeface(Typeface.createFromAsset(getAssets(), "DroidSansJapanese.ttf"));

but that seems like a bit of a kludge. Is there any way to set the font through the xml layout to one that supports japanese?

Spacehamster
  • 125
  • 1
  • 4
  • 8
  • Noticed the same problem on iOS (where I fixed it by setting the font, too). One should have thought that there was enough space in Unicode to give all of those characters their own code-points instead of relying on the application to pick the right font. – Thilo Mar 14 '13 at 12:19
  • Looking at that Wikipedia page, you don't really want to set the font. You want to set the content language, and let the system figure out the proper font variation. In HTML there is a tag for that. Not sure about Android. This is really a mess that should have been fixed in Unicode instead of having to specify the language "out-of-band". – Thilo Mar 14 '13 at 23:34
  • See the answer in https://stackoverflow.com/questions/49277987/display-japanese-style-chinese-letters-instead-of-chinese-style-chinese-letters. It was easier. – Damn Vegetables Mar 14 '18 at 15:52

3 Answers3

5

Generally, older (<4.1) Android devices sold outside of Japan render the Chinese version of kanji by default. Android devices sold inside Japan render the Japanese version of kanji by default. DroidSansJapanese is the Japanese font counter-part to DroidSans and is available in AOSP. It seems like a manufacturer can choose the preference in which system/vendor-provided fonts are used when displaying text, at the time of creating their build of Android. Even if you change your locale on a (<4.1) non-Japanese device, it can still display the incorrect version of the characters.

If you want to localize your app for Japanese users in Japan, they most likely will have a device purchased in Japan and so kanji will display correctly (assuming the manufacturer took the required above action). If this behavior is acceptable to you, then you need not make any changes. If you are building an app targeting users outside of Japan (e.g. people wishing to study Japanese), then this may not sound so good.

Support for Japanese-style kanji improved when Android 4.1 was released. This commit has some detail on what was changed. If your phone is currently using the Japanese locale, Android should display Japanese-style kanji. I have seen this behavior in action with an non-Japanese Samsung Galaxy Note 2 running Android 4.1. When I set my system language to English (UK/US), I see the Chinese-style rendering when looking at kanji text. However, when I set the language to 日本語, I can see the Japanese-style rendering. I have not tested this, but I am wondering if forcing the locale programmatically can also force the display of Japanese-style kanji for 4.1+ devices.

You can't set your font to one you have in the assets via an XML attribute. You have to do this in Java using code similar to what you posted in the original question. To do this globally, you can either make your own subclass of TextView that sets the right font, or you can iterate through all of the views in your layout (e.g. after Activity.setContentView(int)) and update them accordingly if they are TextViews. There is a somewhat more detailed description of this solution in another question. Including a Japanese font will probably bloat your APK by a sizable amount.

If you are writing an app for Japanese users in Japan, I would not bother taking any action since it should already display correctly for them. Users of Android 4.1+ devices also have a good experience, provided they are using the Japanese locale. Otherwise, bundling a font is your answer, and you already have a good idea of what is required.

Community
  • 1
  • 1
antonyt
  • 21,863
  • 9
  • 71
  • 70
2

There's no way to set the font directly in XML.

If you plan on using a lot of Japanese TextViews, it may be easier to create a custom View to handle it for you, which sets the font programmatically from its constructor(s).

Then any time you plop your custom View down in XML, it'll have the correct font.

Geobits
  • 22,218
  • 6
  • 59
  • 103
  • This may be the way to go, because then you can make the View clever enough to consider the content language (which can be different from the locale). – Thilo Mar 14 '13 at 23:35
2

A very similar question is answered here:

Custom Font on Android

You can use some code from GitHub https://github.com/browep/AndroidCustomFontWidgets/ to let you specify a font in the XML.

<com.github.browep.customfonts.view.FontableTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="This is in a custom font"
    app:font="MyFont-Bold.otf" />
Community
  • 1
  • 1
Jonathan Caryl
  • 1,330
  • 3
  • 12
  • 30