Skip to content Skip to sidebar Skip to footer

Japanese Characters Looking Like Chinese On Android

PREAMBLE: since API 17 (Android 4.2), there's a method TextView.setTextLocale() that explicitly solves this problem for TextViews and derived classes. Assign a Japanese locale (Loc

Solution 1:

There are fonts with full Japanese support. I've heard some people talking about DroidSansJapanese.tff and TakaoPGothic.

Solution 2:

Found a somewhat cludgey solution.

The DroidSansJapanese.ttf font exists and is available for download, for example, here. I downloaded it, renamed it to DroidSansJapanese.mp3 to avoid the 1MB compressed asset limit, and placed it under assets/web. I then introduced the following CSS statement:

@font-face
{
font-family: "DroidJP";
src:url('DroidSansJapanese.mp3')
}

Then I added 'DroidJP' to the font-family of every relevant CSS style. The way I load my HTML, the assets/web folder is already designated as the base for loading linked content.

Upon careful examination, I found several places in the app where Japanese was in TextViews. For those, I've loaded the typeface like this:

Typefaces_JFont=
    Typeface.createFromAsset(Ctxt.getAssets(), "web/DroidSansJapanese.mp3");

and called setTypeface() on every relevant TextView. Now for PROFIT!

This expanded my APK by about 1 MB. There was an alternative way, where I'd store the font in the assets in compressed form - that'd save me about 500 KB of APK size, but then I'd have to expand it to phone memory on the first run, worry about data folder path, and lose compatibility with Android 1.5.

Some credit is due: here and here. Does not work on Android 2.1 (the WebViews, not the TextViews) - that's a known bug.

Now, the question remains - how do I identify devices where the default font already contains the Japanese shapes?

EDIT re: the mp3 hack. I've implemented the chunked solution at first, but then decided to go with font in the assets. The chunked approach has one upside - smaller APK size - and the following downsides:

  • Consumes more phone memory - you end up storing both compressed font in assets and uncompressed in the data directory
  • Is incompatible with Android 1.5 on the TextView side - the method Typeface.createFromFile() was introduced in API level 4
  • Complicates HTML generation - the CSS with the @font-face declaration needs to be parametrised, since the data path is a variable
  • Slows down the first app startup - you spend time combining the chunks

Storing the font as a compressed asset is not an option either - the font then doesn't show up in WebView, and you can clearly see in LogCat the message about "Data exceeds UNCOMPRESS_DATA_MAX".

Solution 3:

Using the following in your onCreate/OnCreateView:

AssetManageram= getContext().getApplicationContext().getAssets();
mDroidSansJapaneseTypeface = Typeface.createFromAsset(am, String.format(Locale.JAPAN, "fonts/%s", "DroidSansJapanese.ttf")); //I put the font file in the assets/fonts/ folder

And then for your textviews:

myTextView.setTypeface(mDroidSansJapaneseTypeface);
myTextView.setTextLocale(Locale.JAPAN);

Be aware that not 100% of all fonts will be displayed in Android. I haven't seen too many problems for Japanese characters, but other CJK characters may appear as blank boxes, no matter what ttf file you use.

Post a Comment for "Japanese Characters Looking Like Chinese On Android"