1

I am trying to assign a different font to my project. I want the new font is valid for the entire project, but all I find is to change the font to a textview

Typeface font1 = Typeface.createFromAsset(getAssets(), "fonts/RockFont.ttf");
TextView customText1 = (TextView)findViewById(R.id.text1);
customText1.setTypeface(font1);
customText1.setTextSize(40.f);
customText1.setText("Hello! This is a custom font...");

There any way to default to the entire project a custom font? Best regard

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
user986689
  • 111
  • 10

2 Answers2

0

Not exactly what you asked for, but building on the comment above there are ways to make using a custom font with default controls easier.

This shows how to extend TextView and use a custom attribute so the TextView supports a custom font.
Custom fonts and XML layouts (Android)

Community
  • 1
  • 1
Theo
  • 5,963
  • 3
  • 38
  • 56
0

What I do is create a support class and instantiate it from my activity and pass through all the views I wish to style.

public class textfactory{
private TextView tv;
private Button b;
private RadioButton rb;

private TypeFace font;

/**
* fetch font resource
*/
public textfactory(Context context){
    this.font = Typeface.createFromAsset(context.getAssets(), 'customfont.ttf');
}
/**
* pass in all the views you wish to apply font to
*/
public void style(View... views){
    for(View v : views){
        if(v instance of TextView)
        {
            tv = (TextView)v;
            tv.setTypeface(this.font);
        }
        else if(v instance of Button)
        {
            b = (Button)v;
            b.setTypeface(this.font);
        }
        else if(v instance of RadioButton)
        {
            rb = (RadioButton)v;
            rb.setTypeface(this.font);
        }
        //add as many view conditionals as required
    }
}
}
JackMahoney
  • 3,423
  • 7
  • 32
  • 50