Question

I add Linear Layout and multiple textviews programmatically and set custom font to texts,

i did it in the bellow way and its works perfectly .

MainActivity 1:

 public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

     LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);

        TextView tv = new TextView(this);
        tv.setGravity(Gravity.RIGHT);
        tv.setTextColor(Color.GREEN);
        tv.setTextSize(40);    
        ll.addView(tv);
        Typeface face4=Typeface.createFromAsset(getAssets(),"BFantezy.ttf");     
        tv.setTypeface(face4);
        tv.setText(Html.fromHtml(getString(R.string.trip)));    

        ImageView divider = new ImageView(this);
        LinearLayout.LayoutParams lp = 
         new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
        lp.setMargins(10, 10, 10, 10);
        divider.setLayoutParams(lp);
        divider.setBackgroundColor(Color.BLUE);
        ll.addView(divider);

        TextView tv1 = new TextView(this);      
        tv1.setGravity(Gravity.RIGHT);
        tv1.setTextSize(40);
        ll.addView(tv1);
        Typeface face1=Typeface.createFromAsset(getAssets(),"BFantezy.ttf");     
        tv1.setTypeface(face1);
        tv1.setText(Html.fromHtml(getString(R.string.trip1)));

        ImageView divider1 = new ImageView(this);
        LinearLayout.LayoutParams lp1 = 
         new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
        lp1.setMargins(10, 10, 10, 10);
        divider1.setLayoutParams(lp1);
        divider1.setBackgroundColor(Color.BLUE);
        ll.addView(divider1);

        TextView tv2 = new TextView(this);
        tv2.setGravity(Gravity.RIGHT);
        tv2.setTextSize(40);
        ll.addView(tv2);
        Typeface face2=Typeface.createFromAsset(getAssets(),"BFantezy.ttf");     
        tv2.setTypeface(face2);
        tv2.setText(Html.fromHtml(getString(R.string.trip2)));

        ImageView divider3 = new ImageView(this);
        LinearLayout.LayoutParams lp3 = 
         new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
        lp3.setMargins(10, 10, 10, 10);
        divider3.setLayoutParams(lp3);
        divider3.setBackgroundColor(Color.BLUE);
        ll.addView(divider3);       
                         }
                  }

but as recall

Typeface.createFromAsset(getAssets()

with each text ,its an heavy way to apply custom font , I try to make custom view as bellow but it doesn't set the custom font to texts:

MainActivity:

  public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);

    // add text view
    TextView tv = new TextView(this);
    tv.setGravity(Gravity.RIGHT);
    tv.setTextColor(Color.GREEN);
    tv.setTextSize(40);    
    ll.addView(tv);
    tv.setText(Html.fromHtml(getString(R.string.trip)));    

    ImageView divider = new ImageView(this);
    LinearLayout.LayoutParams lp = 
     new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
    lp.setMargins(10, 10, 10, 10);
    divider.setLayoutParams(lp);
    divider.setBackgroundColor(Color.BLUE);
    ll.addView(divider);


    TextView tv1 = new TextView(this);      
    tv1.setGravity(Gravity.RIGHT);
    tv1.setTextSize(40);
    ll.addView(tv1);
    tv1.setText(Html.fromHtml(getString(R.string.trip1)));

    ImageView divider1 = new ImageView(this);
    LinearLayout.LayoutParams lp1 = 
     new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
    lp1.setMargins(10, 10, 10, 10);
    divider1.setLayoutParams(lp1);
    divider1.setBackgroundColor(Color.BLUE);
    ll.addView(divider1);


    TextView tv2 = new TextView(this);
    tv2.setGravity(Gravity.RIGHT);
    tv2.setTextSize(40);
    ll.addView(tv2);
    tv2.setText(Html.fromHtml(getString(R.string.trip2)));

    ImageView divider3 = new ImageView(this);
    LinearLayout.LayoutParams lp3 = 
     new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
    lp3.setMargins(10, 10, 10, 10);
    divider3.setLayoutParams(lp3);
    divider3.setBackgroundColor(Color.BLUE);
    ll.addView(divider3);
}}

CustomTextView:

    public class CustomTextView extends TextView {

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        //call the constructor which has the complete definition
        this(context, attrs, 0);
        init();
    }

    public CustomTextView(Context context) {
        super(context);
        init();
    }

    private void init() {

            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "BFantezy.ttf");
            setTypeface(tf);
        }
    }

any help will be appreciated , thanks.

Was it helpful?

Solution

Instead of creating your font from the assets every time, create a "FontFactory" and reuse the same one.

public class FontFactory {

private static Typeface t1;

public static Typeface getBFantezy(Context c) {
    if (t1 == null) {
        t1 = Typeface.createFromAsset(c.getAssets(), "BFantezy.ttf");
    }
    return t1;
}


private static Typeface t2;

public static Typeface getOtherFont(Context c) {
    if (t2 == null) {
        t2 = Typeface.createFromAsset(c.getAssets(), "OtherFont.ttf");
    }
    return t2;
}
}

Then to use it in your code you would just :

tv1.setTypeface(FontFactory.getBFantezy(getContext());

You no longer have to worry about creating the typeface. If it needs it, the factory will create it. You can add similar methods to handle any other typefaces you wish to use.

OTHER TIPS

You have created a custom TextView class and forgot to implement it.. try

CustomTextView tv1 = new CustomTextView(this); 

instaed of

TextView tv1 = new TextView(this); 

Likewise to all other TextView's

Hope this helps....

You don't have to create a Typeface for each TextView. You could do something like Typeface face=Typeface.createFromAsset(getAssets(),"BFantezy.ttf"); and after that for each TextView you can do textview.setTypeface(face). Only call the method createFromAsset() once and reuse the object you get.

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