3

I need to show TextView in malayalam font which is an indian local language. For this I am trying below code

        Typeface custom_typerface = Typeface.createFromAsset(getAssets(), "fonts/AnjaliNewLipi.ttf");
        TextView mytext = (TextView)findViewById(R.id.mytext);
        mytext.setTypeface(custom_typerface);
        mytext.setText("മലയാള ഭാഷ തന്‍ ഭാവ ശുദ്ധി...");

This shows correctly on a 4.2 device, but not in all devices. How can I render it for all android devices? Any help will appreciated.

COD3BOY
  • 11,964
  • 1
  • 38
  • 56
Alex Chengalan
  • 8,211
  • 4
  • 42
  • 56

4 Answers4

2

Above code is correct and it should show Malayalam fonts. But there is a catch. if you phone doesn't support the font, it wont show Malayalam, but will show some boxes instead.

If you use any file browser and look into system/system/fonts - u will see all fonts supported by device.

If you want to add new fonts you can do that as well, but device has to be rooted for that. Many of the cheap phones just support few fonts, but costly phones have support for many fonts.

Pankaj Kushwaha
  • 300
  • 1
  • 13
0

At last I found the answer after a long search.. I converted Unicode fonts to Ascii,by replacing each and every Unicode character with Ascii. Here is the magic code

mytext.replaceAll("oldChar", "newChar");

use this code for each and every single character. :)

Alex Chengalan
  • 8,211
  • 4
  • 42
  • 56
  • 1
    This is a very strange answer since ASCII contains no indian characters. – Sulthan Apr 27 '13 at 11:07
  • @Sulthan But we can replace any character with this simple code. :) – Alex Chengalan Apr 27 '13 at 11:22
  • Unicode contains over 100 000 characters, ASCII contains 256 characters. How do you convert Unicode to ASCII? Please, extend your question to show what wasn't worning and explain what exactly had you to do to make it working. This question/answer won't be helpful for future visitors. – Sulthan Apr 27 '13 at 11:54
  • use your own typeface and convert each and every unicode characters in it with ascii. for example, in my case `my_text.toLowerCase().replaceAll("അ", "A").replaceAll("ആ","B")`, use code like this for all characters... – Alex Chengalan Apr 28 '13 at 02:03
  • Does it involve editing the font (`.ttf`) file? If yes, then it's not a good answer because it requires specialized tools. – Sulthan Apr 28 '13 at 08:24
  • It does not involve editing ttf. You just need to do within Java code. This is actually like a preprocessing. You need to set custom typeface to the textview, then replace the characters like the above, then set the final string on the text view. I do this for tamil bamini font. But, @Alex, how did you get the equivalent ASCII characters for each of your malayalam characters? Because, now I am having trouble with Kannada fonts. Please have a look at http://stackoverflow.com/questions/16455923/kannada-font-on-android-ics – Karthik Andhamil May 09 '13 at 17:08
  • My answer worth atleast a mension bro :) That code was really a hardwork :) – Basim Sherif May 10 '13 at 09:16
0

Set custom font to TextView.

You have two ways, first see first way:

1. Add your font files to assets folder, if not exist assets folder create its.

2. Create CustomTypeface class, for examples:

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Typeface;

public class CustomTypeface {

    private static Typeface typefaseArialCondIt;
    private static Typeface typefaseArialBold;
    private static Typeface typefaseArialRegular;

    public CustomTypeface() {
    }

    public CustomTypeface(Context context) {
        AssetManager assets = context.getAssets();
        CustomTypeface.typefaseArialCondIt = Typeface.createFromAsset(assets, "fonts/ArialCondIt.otf");
        CustomTypeface.typefaseArialBold = Typeface.createFromAsset(assets, "fonts/ArialBold.otf");
        CustomTypeface.typefaseArialRegular = Typeface.createFromAsset(assets, "fonts/ArialRegular.otf");
    }

    public static Typeface gettypefaseArialCondIt() {
        return typefaseArialCondIt;
    }

    public static Typeface settypefaseArialCondIt(Typeface typefaseArialCondIt) {
        return CustomTypeface.typefaseArialCondIt = typefaseArialCondIt;;
    }

    public static Typeface gettypefaseArialBold() {
        return typefaseArialBold;
    }

    public static Typeface settypefaseArialBold(Typeface typefaseArialBold) {
        return CustomTypeface.typefaseArialBold = typefaseArialBold;;
    }

    public static Typeface gettypefaseArialRegular() {
        return typefaseArialRegular;
    }

    public static void settypefaseArialRegular(Typeface typefaseArialRegular) {
        CustomTypeface.typefaseArialRegular = typefaseArialRegular;
    }
}

3. And set custom font to TextView:

TextView yourTextView = (TextView) findViewById(R.id.textView1);
yourTextView.setTypeface(CustomTypeface.gettypefaseArialRegular);

Or second way, create custom TextView with your fonts:

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class ArialTextView extends TextView {

    public ArialTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public ArialTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public ArialTextView(Context context) {
        super(context);
    }

    @Override
    public void setTypeface(Typeface tf, int style) {
        if (style == Typeface.BOLD) {
            setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/ArialBold.otf"));
        } else if (style == Typeface.NORMAL) {
            setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/ArialRegular.otf"));
        } else if (style == Typeface.ITALIC) {
            setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/ArialCondIt.otf"));
        }
    }
}

And in the layout xml you can create custom TextView, for example:

<com.your.package.ArialTextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textAppearance="?android:attr/textAppearanceMedium"
     android:text="TextView with your custom font"/>
SBotirov
  • 13,872
  • 7
  • 59
  • 81
  • if your android is AnkiDroid, then see this link:https://code.google.com/p/ankidroid/wiki/UsingCustomFonts – SBotirov Apr 28 '13 at 05:17
0

I am not sure if this might work in your case but I worked with Punjabi font. What I did was not copied or entered the text into the string and then use the typeface. You certainly do use the typeface but in order for it to work properly you need to enter the Malayalam text in Transliteration i.e. as in the following example in Hindi as I don't know Malayalam.

PUl

 Typeface custom_typerface = Typeface.createFromAsset(getAssets(), "fonts/AnjaliNewLipi.ttf");
    TextView mytext = (TextView)findViewById(R.id.mytext);
    mytext.setTypeface(custom_typerface);
    mytext.setText("PUl"); //Assuming you know Hindi PUl in hindi is फूल

This will show the text in proper format with no issues however if you simply just enter

mytext.setText("फूल");  

It is highly possible that it won't render the font properly. I hope this helps. :)

ivesingh
  • 888
  • 3
  • 12
  • 30