4

I have a hindi content based android app and have used devangiri font from Android API 16 SDK and renamed as hindi.ttf. The text renders fine on API level 16 and 17 but simply tears apart in Android API level 15 and lower.

Is there any why I can fix that without removing support for lower API levels. My code for setting textview is:

import android.app.Activity;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.GestureDetector;
import android.widget.TextView;

public class Prayer extends Activity {

    // TextSwitcher vs;
    GestureDetector gestureDetector;
    static Tips tip = null;
    static StringBuilder quesString = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.prayer);

        TextView tv1 = (TextView) findViewById(R.id.textView1);
        Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/hindi.ttf");
        tv1.setTypeface(tf);
        tv1.setText("श्री");
        tv1.setPaintFlags(tv1.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }
}

Layout XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#C5C6E1"
    tools:context=".Prayer" >
            <TextView
                android:id="@+id/textView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </TextView>
</RelativeLayout>

Current: enter image description here

Expected: enter image description here

  • use activity context instead of application context http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context. You can set color as tv.setTextColor(android.R.color.black); Can u post a snap shot. It should work fine – Raghunandan Apr 19 '13 at 15:32
  • at least it was showing something with getAppContext but with CurrentActivity.this, no text comes up on the screen. has anyone tried showing hindi text in android 4.0.2 (API-15) – Piyush-Ask Any Difference Apr 19 '13 at 16:15
  • have you set the textview by setContentView(tv1)?. I have 4.0.2 aand i use activity context works fine – Raghunandan Apr 19 '13 at 16:17
  • yes that is there. The problem is that the text is not rendered properly. While it should be as shown in screen1, it is coming as shown in screen2. Look at words like shri which has wrong hindi spelling in screen2. Have attached screenshots in the question. Is that a font file issue? – Piyush-Ask Any Difference Apr 19 '13 at 16:29
  • Will it make sense to capture screen images for various resolution from app running on android jellybean and then show it if user is having anything less than API 16 – Piyush-Ask Any Difference Apr 20 '13 at 10:38
  • Try loading the text in a webview. – Raghunandan Apr 20 '13 at 10:47
  • Still no luck. problem is the font which results in same issue whether textview or webview – Piyush-Ask Any Difference Apr 21 '13 at 08:46
  • i don't know cause i cannot duplicate the problem – Raghunandan Apr 21 '13 at 08:51
  • the second screen image is very bad. change it if you can. – Mbt925 Apr 27 '13 at 21:03

2 Answers2

5

I faced exactly similar issue when I was developing an Hindi app. However, the error resolved when I changed the font files.

Also, IMHO the device simply replaces UTF-8 symbols in text with corresponding symbols in font files. In this case, its showing a completely different symbol altogether. So, the problem should be with font files.

Give me your e-mail in case you want to use my font files.

Gaurav Arora
  • 17,124
  • 5
  • 33
  • 44
3

For Hindi support, I backported the DroidSansDevanagari-Regular.ttf font that was added in Jellybean. You can find all the Jellybean Android fonts in you SDK directory under:

/<Android SDK Dir>/platforms/android-16/data/fonts

If you are still seeing tearing on 4.0.2, you can try:

tv1.setPaintFlags(tv1.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);

When setting a dynamic textSize use this:

tv1.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelOffset(R.dimen.questionTEXT));
JustinMorris
  • 7,259
  • 3
  • 30
  • 36
  • still no luck. the Full code I am using now is: setContentView(R.layout.prayer); TextView tv1 = (TextView) findViewById(R.id.textView1); tv1.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/DroidSansDevanagari-Regular.ttf")); tv1.setPaintFlags(tv1.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG); tv1.setTextColor(getResources().getColor(android.R.color.black)); tv1.setTextSize(getResources().getDimension(R.dimen.questionTEXT)+2); tv1.setMovementMethod(new ScrollingMovementMethod()); tv1.setText("श्री बद्रीनारायण जी की आरती"); – Piyush-Ask Any Difference Apr 20 '13 at 00:47
  • I have striped my code but still seeing issues. See my original post for the current bare minimum code I am using. – Piyush-Ask Any Difference Apr 23 '13 at 02:18