11

I am having a TextView that has a huge text in between i have a tamil word and i know how to embedd the tamil font in seperate textview .but i need the tamil word between english word please help thanks in advance

my part of text in textview :

Seasonal messages like welcome (நல்வரவு) is used in Kolam. Volunteering to draw kolam at temple is sometimes done when a devotee's

Imran Rana
  • 11,899
  • 7
  • 45
  • 51
Thiru VT
  • 831
  • 2
  • 8
  • 24
  • hey you just want to show text in textview only na?? no other operations are done on it??? – user May 17 '12 at 12:13
  • yes i want to display only text ,when i try to split text in different textviews i am not getting scroll view – Thiru VT May 17 '12 at 12:16
  • look at this http://stackoverflow.com/a/8471977/1012284 – Padma Kumar May 17 '12 at 12:25
  • i am unable to get tamil font face from html string – Thiru VT May 17 '12 at 12:35
  • Hi. I am trying to follow your approach, but still as you could see, the word 'வு' is not displayed correctly. Not only this , but also there are other words which gets jumbled. Do you have any idea or how I can display it correctly. – Andro Selva Jun 13 '12 at 06:01

4 Answers4

5

Try setting this Akshar.ttf font to your TextView through setTypeface, it suuports both English and Tamil.


Here is the outcome:

enter image description here

Or, look for a similar font which supports both language.


your second solution is to use image for this small Tamil text portion using SpannableStringBuilder.


Code for setting custom font to TextView:

Assuming you have the Akshar.ttf font in fonts folder under assets folder:

 Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Akshar.ttf");               
    TextView tv = (TextView) findViewById(R.id.CustomFontText);
    tv.setTypeface(tf);
    tv.setText("Seasonal messages like welcome (நல்வரவு) is used in Kolam. Volunteering to draw kolam at temple is sometimes done when a devotee's");
Community
  • 1
  • 1
Imran Rana
  • 11,899
  • 7
  • 45
  • 51
  • @ThiruVT take a look at my another answer, it is more generalized :) – Imran Rana May 24 '12 at 15:47
  • Hi. I am trying to follow your approach, but still as you could see, the word 'வு' is not displayed correctly. Not only this , but also there are other words which gets jumbled. Do you have any idea or how I can display it correctly. – Andro Selva Jun 13 '12 at 06:01
  • @AndroSelva complex glyphs are shown as broken characters. So, I used text to image replacing for my Bangla IME to show those complex glyphs and used overlay technique to reduce no of required images. – Imran Rana Jun 14 '12 at 21:09
2

There is another solution if you want to support multiple fonts within a TextView but they are not supported within a single ttf:

Use the following code:(I'm using Bangla and Tamil font)

  TextView txt = (TextView) findViewById(R.id.custom_fonts);  
        txt.setTextSize(30);
        Typeface font = Typeface.createFromAsset(getAssets(), "Akshar.ttf");
        Typeface font2 = Typeface.createFromAsset(getAssets(), "bangla.ttf");   
        SpannableStringBuilder SS = new SpannableStringBuilder("আমারநல்வரவு");
        SS.setSpan (new CustomTypefaceSpan("", font2), 0, 4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        SS.setSpan (new CustomTypefaceSpan("", font), 4, 11,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        txt.setText(SS);

The outcome is:

enter image description here


CustomTypefaceSpan Class:

package my.app;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;

public class CustomTypefaceSpan extends TypefaceSpan {

private final Typeface newType;

public CustomTypefaceSpan(String family, Typeface type) {
    super(family);
    newType = type;
}

@Override
public void updateDrawState(TextPaint ds) {
    applyCustomTypeFace(ds, newType);
}

@Override
public void updateMeasureState(TextPaint paint) {
    applyCustomTypeFace(paint, newType);
}

private static void applyCustomTypeFace(Paint paint, Typeface tf) {
    int oldStyle;
    Typeface old = paint.getTypeface();
    if (old == null) {
        oldStyle = 0;
    } else {
        oldStyle = old.getStyle();
    }

    int fake = oldStyle & ~tf.getStyle();
    if ((fake & Typeface.BOLD) != 0) {
        paint.setFakeBoldText(true);
    }

    if ((fake & Typeface.ITALIC) != 0) {
        paint.setTextSkewX(-0.25f);
    }

    paint.setTypeface(tf);
}
}

Reference

Community
  • 1
  • 1
Imran Rana
  • 11,899
  • 7
  • 45
  • 51
2

I had this problem in Android 4.0. I found that this is due to csc(Country Code). If it is a India then Tamil working correctly else not working. I can confirm this.

Satheesh Kumar
  • 109
  • 1
  • 5
2

Till ICS (4.0) there were no Tamil font available in Android Systems. Even then it had bugs and Google fixed it all with Jelly Bean (4.2).

Now if you wanna show Tamil in your application you need to use TAB, TAM, BAMINI or TSCII encodings.

I wrote a simple library that does the conversion for you dynamically. All you have to write is simple code as below.

// Initialise the Typeface (assumes TSCII, Bamini, Anjal, TAB or TAM font located inside assets/fonts folder)
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/mylai.ttf");
// Initialises the TextView
TextView tv = (TextView)findViewById(R.id.textView1);
//Setting the Typeface
tv.setTypeface(tf);
//Magic happens here ;) encoding conversion
String TSCIIString = TamilUtil.convertToTamil(TamilUtil.TSCII, "வணக்கம் அன்ரொயிட்");
//Setting the new string to TextView
tv.setText(TSCIIString);

I have written more on this topic in this answer. Please check it out too.

Community
  • 1
  • 1
Jay Mayu
  • 17,023
  • 32
  • 114
  • 148