Question

I have recently come across a situation while developing an app where i have to display different languages in text view . currently I am displaying few using fonts/typeface like this :

Typeface tf = Typeface.createFromAsset(this.getAssets(),
                "DroidHindi.ttf");
        TextView textView1 = (TextView) findViewById(R.id.textView2);
        textView1.setTypeface(tf);
        textView1.setText("कचजड, कचजड");

        Typeface tf1 = Typeface.createFromAsset(this.getAssets(),
                "asunaskh.ttf");
        TextView textView = (TextView) findViewById(R.id.textView1);
        textView.setTypeface(tf1);
        textView.setText("یہ انگریزی نہیں");

        Typeface tf2 = Typeface.createFromAsset(this.getAssets(),
                "Banglafont.ttf");
        TextView textView2 = (TextView) findViewById(R.id.textView3);
        textView2.setTypeface(tf2);// এই ইংরেজি নয়
        textView2.setText("এই ইংরেজি নয়");

its fine my question is that i have to support for some 20 different languages then things will become very tedious when i apply this in different activities . Any alternative way to achieve.

Was it helpful?

Solution

You do is to create a class and a function with access public that will return you TextView Object with the Font that you want to suppose.

    TextView public SetLanguage(TextView tv,string type)
    {
     TextView newtv = tv;
     Typeface tf;
     switch(type)
     {
      case "urdu":
         tf = Typeface.createFromAsset(this.getAssets(),"urdu.ttf");
         break;
      case "hindi":
         tf = Typeface.createFromAsset(this.getAssets(),"hindi.ttf");
         break;
     //   up so on                

   }
   newtv.setTypeface(tf);
   return newtv;
 }
  // and call it any where..
 TextView textView1 = (TextView) findViewById(R.id.textView2);
 textView1 = classobj.SetLanguage(textView1,"urdu");
 //assign string of text to it

OTHER TIPS

Initialize your typefaces when the app starts up, and make a method which takes any view and sets the typeface based on the language.

Just like we do with other resource files, we can also define a font directory for any locale or country code we want to. The downside to this approach is that both the fonts have to be named the same although they are different but otherwise a clean solution.

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top