user2365658
user2365658

Reputation: 41

Farsi characters not displaying in Android app

I am creating stream based farsi application for android 3.0. It is displaying 'squares' at the places of characters گ ڪ ڙ ا

How can I add support for farsi?

Upvotes: 1

Views: 523

Answers (3)

user1831682
user1831682

Reputation:

First Download the ttf files from any one of the links,

http://www.neda.net.ir/downloads/fonts/persian.ttf

http://alefba.us/wp-content/plugins/download-monitor/download.php?id=farsifonts-0.4.zip

 copy the ttf file into assets folder in your project,,

first select the Languge code as Farsi in code on clicking button

button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Locale myLocale = new Locale("fa"); 
            Resources res = getResources(); 
            DisplayMetrics dispmetrics = res.getDisplayMetrics(); 
            Configuration conf = res.getConfiguration(); 
            conf.locale = myLocale; 
            res.updateConfiguration(conf, dispmetrics);
            String current_locale = getResources().getConfiguration().locale.getDisplayName();
            System.out.println(current_locale);
            Intent refresh = new Intent(getBaseContext(), MainActivity.class);
            startActivity(refresh);
        }
    });

then below , setContentView(R.layout.activity_main);

              implement this code,,to get the typeFace done

            String current_locale = getResources().getConfiguration().locale.getDisplayName();
    System.out.println("-----"+current_locale);

    if(current_locale.equals("Persian"))
    {

    Typeface font1 = Typeface.createFromAsset(getAssets(), "persian.ttf");
    b1.setTypeface(font1);
    }

this solution definitely works and the reason that square brackets appears in your phone is due to that Farsi Unicode is not installed on your Mobile and if u want to install Farsi font your phone must be rooted one

--Thanks

Upvotes: 0

Maulik Sheth
Maulik Sheth

Reputation: 574

you need to make use of Typeface to support additional fonts.

create a new folder called assets(if it doesnot exists). create a new folder in it called fonts(just for your simplicity)

download the 'farsi' fonts from the internet and copy it in the fonts folder.

in your class file create a String to the path of the font. and use this code

    Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
    txt1.setTypeface(tf);

Edit: some fonts may support in one mobile and in other it might not. for example hindi font is supported in my xperia but not in my friends S2.

Upvotes: 1

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

It means that fonts your device uses do not support farsi characters (hence squares). You need to find better font and use it in your application (see http://developer.android.com/reference/android/graphics/Typeface.html docs)

Upvotes: 2

Related Questions