Search code examples
androidandroid-listviewtextviewtruetype

How to implement custom font in android


I am implementing a custom listview using custom array adpter. In each row, there is a text view and a image. I want to add custom font for arabic on the text view. can anyone help me load custom font. Thanks in advance for any help.

Here is the code of MyViewHolder where i am creating the text view object :

public static class MyViewHolder {
    TextView textViewArabic;
    ImageView imgView;

    public MyViewHolder(View v, Context context) {
        // TODO Auto-generated constructor stub
        textViewArabic = (TextView) v.findViewById(R.id.tvArabic);
        imgView = (ImageView) v.findViewById(R.id.imgView);
    }
}

Solution

  • Download a TruTypeFace font (.ttf) and place it in asset folder. Add following code to get that font from assets set that font to your text view like the code below.

    public static class MyViewHolder {
      TextView textViewArabic;
      ImageView imgView;
      Typeface face;
    
      public MyViewHolder(View v, Context context) {
       // TODO Auto-generated constructor stub
       textViewArabic = (TextView) v.findViewById(R.id.tvArabic);
       imgView = (ImageView) v.findViewById(R.id.imgView);
       face = Typeface.createFromAsset(context.getAssets(),
                        "fonts/_PDMS_Saleem_QuranFont.ttf");
       textViewArabic.setTypeface(face);
      }
     }