-2

i create an Android app Activity which contain Arabic Text and i set Typeface on this text like as

 Typeface tf= Typeface.createFromAsset(getAssets(), "trado.ttf");
 textView.setTypeface(tf);

but this Typeface not supporting all deveice's. On Samsun S4 its work correcctly but on Samsung grand it's not work.

Screenshot of Samsung galaxy s4 which show Arabic Text properly

enter image description here

Screenshot of Samsung grand which can't show Arabic Text properly

enter image description here

i am attach both screen shot please check it and tell me, how i can handle this issue.

King of Masses
  • 18,405
  • 4
  • 60
  • 77
Jawad Ali
  • 19
  • 3
  • Use RTL Layout and check it – Somasundaram NP Aug 10 '15 at 10:47
  • Not Placement Issu ,i already use RTL layout but not working. – Jawad Ali Aug 10 '15 at 10:53
  • more information might be helpful - what's not working? did you get any exception on this code line? or on other lines? is the text not showing? or showing gibberish? is it a text you receive from an outer source? (e.g. some web service) or is it hard coded, we really need more info in order to help – TommySM Aug 10 '15 at 11:14
  • @JawadAli how did you resolved the problem I have same problem in implementing nastaleeq font on URDU language. text just got over lapped in textview. please help me – Allay Khalil Feb 26 '16 at 13:42

2 Answers2

0

You should cache the TypeFace, otherwise you might risk memory leaks on older handsets. Caching will increase speed as well since it's not super fast to read from assets all the time.

public class FontCache {

    private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>();

    public static Typeface get(String name, Context context) {
        Typeface tf = fontCache.get(name);
        if(tf == null) {
            try {
                tf = Typeface.createFromAsset(context.getAssets(), name);
            }
            catch (Exception e) {
                return null;
            }
            fontCache.put(name, tf);
        }
        return tf;
    }
}

I gave a full example on how to load custom fonts and style textviews as an answer to a similar question. You seem to be doing most of it right, but you should cache the font as recommended above.

Calligraphy is looks for the font in a pretty specific order, for the most part it's very similar to how the Android framework resolves attributes.

Define your default font using CalligraphyConfig, in your Application class in the #onCreate() method.

@Override
public void onCreate() {
    super.onCreate();
    CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                            .setDefaultFontPath("fonts/Roboto-RobotoRegular.ttf")
                            .setFontAttrId(R.attr.fontPath)
                            .build()
            );
    //....
}

Initially i recommended to try the 1st approach, still if you faced the problem then go for 2nd approach (3rd party library)

Hope this will helps

Community
  • 1
  • 1
King of Masses
  • 18,405
  • 4
  • 60
  • 77
  • show Syntax error on textView.setTypeface(Typeface.defaultFromStyle(required int value ), tf); – Jawad Ali Aug 10 '15 at 11:05
  • sorry for unable to help :( let see some one will come up with solution – King of Masses Aug 10 '15 at 11:36
  • now i attach images ,one image show Arabic Text properly in S4 and in Samsung Grand not work properly. please check it and help me to solving the problem . @King of Masses – Jawad Ali Aug 10 '15 at 12:08
0

I think create value-arabic-> strings.xml and write here your text. It will be help

Madi
  • 1,805
  • 1
  • 15
  • 9