Search code examples
androidunicode

How to display Sinhala Unicode characters in android app


I,m trying to show Sinhala Unicode characters in android app. when i am using Samsung tabs or phones Unicode is running it on without problem.but it does not work on other phones or tab.cause there is no Sinhala Unicode.How to do this for an example i run this code on Samsung tab successfully.

Toast.makeText(this, "අන්තර්ජාල සම්බන්දතාවය තිබේ ", Toast.LENGTH_SHORT).show();

but other phones or tab does n't work.


Solution

  • You might need to have a Sinhalese font file (say, Sinhalese.ttf) in the directory assets/fonts in the root of your project. You create a dummy text view because it has a method called setTypeface, which sets the font for next code:

    import android.graphics.Typeface;
    
    public class FontSampler extends Activity {
      @Override
      public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
    
        TextView tv=(TextView)findViewById(R.id.custom);  // a dummy text view
        Typeface face=Typeface.createFromAsset(getAssets(),
                                          "fonts/Sinhalese.ttf");
        tv.setTypeface(face);
    
        Toast.setView(tv).makeText(this, "අන්තර්ජාල සම්බන්දතාවය තිබේ ", Toast.LENGTH_SHORT).show();
      }
    }
    

    This way, even if an android phone doesn't have a font installed, then the font embedded in your app will serve. Hope this helps.