0

I want to use custom font in my application. Which is the best way to give font to entire application. I know how to assign custom font to a single TextView or Button.

Is it possible to mention the custom font in one place, for eg. in styles.xml, and the font will be applied to whole application (every TextView, Button, EditText and soon).

Nitesh Kumar
  • 5,370
  • 5
  • 37
  • 54
  • You can create your own Theme and apply it to the app in your Manifest ``. More info here: http://developer.android.com/guide/topics/ui/themes.html but it takes too many time :) – CaptainTeemo Nov 12 '14 at 09:59
  • possible duplicate of [Custom Fonts and Custom Textview on Android](http://stackoverflow.com/questions/8954484/custom-fonts-and-custom-textview-on-android) or [Add custom font for complete android application](http://stackoverflow.com/questions/6926263/add-custom-font-for-complete-android-application) – Selvin Nov 12 '14 at 10:04

2 Answers2

0

use this type of textview in your app

public class MyTextView extends TextView {

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

public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

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

public void init() {
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf");
    setTypeface(tf ,1);

}

}

also edit text

public class CEditText extends EditText {


private Context context;
private AttributeSet attrs;
private int defStyle;

public CEditText(Context context) {
    super(context);
    this.context=context;
    init();
} 

 public CEditText(Context context, AttributeSet attrs) {
      super(context, attrs);
      this.context=context;
      this.attrs=attrs;
      init();
 }

public CEditText(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      this.context=context;
      this.attrs=attrs;
      this.defStyle=defStyle;
      init();
}

private void init() {
      Typeface font=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
      this.setTypeface(font);
}
@Override
public void setTypeface(Typeface tf, int style) {
    tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
    super.setTypeface(tf, style);
}

@Override
public void setTypeface(Typeface tf) {
    tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
    super.setTypeface(tf);
}
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
  • Also, in layout xml file it should be loaded with – Rames Palanisamy Nov 12 '14 at 09:57
  • I know this approach, thanks anyway. What about Buttons and EditText? Do I have to make custom EditText and Buttons as well? – Nitesh Kumar Nov 12 '14 at 09:58
  • yes this is called coustem view in android use any xml file – Naveen Tamrakar Nov 12 '14 at 09:58
  • I love when people posting someone else's answer as their own ... instead marking question as duplicate http://stackoverflow.com/questions/6926263/add-custom-font-for-complete-android-application#answer-8676413 I even gave name to it: stealing – Selvin Nov 12 '14 at 10:08
0

Unfortunatly, android is not providing any method to apply custom font for the complete app in one place.

However, you can create your CustomTextView extends TextView and CustomButton extends Button. In that you can set the font by creating a FontInstance.