Feb 10, 2012

Hindi on an Android Phone

This is just an app I am developing on the side. It uses Devanagari scripts to render the text, rather than displaying images as most of the similar apps in the Android Markets do.

Part 1 of the problem was how to load the hindi font in the code and get the display to render it. If you try this on a phone without Hindi support - which is the vast majority of the phones out there, then all you see is squares instead of the text.



This was pretty simple. If you want to load a custom font, this is what you do :


  1. Get the specific font you want to bundle with your application. Lets call it Hindi.ttf
  2. Put it in your project under the "assets/fonts" folder. You will need to create the folder 'fonts'. Not necessary to have the exact name, but I use it as it denotes what the files int the folder are. 
  3. Now you need to tweak your code a bit as you need to load the font before you render the text. This is quite simple - use something like the following :


//Font work
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Hindi.ttf");
// Get the UI element handles
mText = (TextView)findViewById(R.id.body_text);
// Set the title and read the file for the text
mText.setText(readRawTxt(pName));
mText.setMovementMethod(new ScrollingMovementMethod());
mText.setTypeface(tf); // setting font for the text. 
 Now when you run the program, you will get the fonts loaded and rendered, which brings use to Part 2 of the problem.

Part 2 of the problem is that the maatras are not getting aligned properly. For this I have so far not been able to figure out how to fix it. If you know , let me know. If I get to know about it, I will update this post.



4 comments:

  1. I have exect problem.
    Did You find any solution.
    From last 2 days I am looking for solution.
    Now its urgent for me.
    It will be so kind of you, if you could help me here.

    ReplyDelete
  2. The solution is that the phone needs to have support for indic text else it does not render it correctly. I changed the ROM on my phone from Cyanogenmod to Miui ( Myth Ace ) and it is working fine now.

    ReplyDelete
  3. but I can not change anyones phone configuration.It has to be through my application somehow.

    ReplyDelete
  4. The rendering engine in the OS needs to be capeable of rending indic correctly. If that is not there, then you will need to write your own renderer. Other than that , no other way that I could figure out.

    ReplyDelete