I want to make an app in a non English language. So I need the textviews, strings and the toast to be in the non-English language. Setting the typeface for every text is cumbersome. Is there a way to set a default font? When I have to refer to something in English (like email id) then I can use the textview.settypeface(xyz) method.
Asked
Active
Viewed 4,237 times
7
-
you can try this link https://coderwall.com/p/qxxmaa/android-use-a-custom-font-everywhere – Rizky Ariestiyansyah Jan 03 '16 at 02:57
2 Answers
9
There is a grate library for custom fonts in android: custom fonts
Here is a sample how to use it.
In gradle you need to put this line:
compile 'uk.co.chrisjenx:calligraphy:2.1.0'
Then make a class that extends application and write this code:
public class App extends Application {
@Override public void onCreate() {
super.onCreate();
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("your font path")
.setFontAttrId(R.attr.fontPath)
.build()
);
}
}
In the activity class put this method before onCreate:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
In your manifest file write like this:
<application
android:name=".App"
It will change the whole activity to your font!. I'ts simple solution and clean!
-
1Here's a video of the guy who made the library explaining how it works https://www.youtube.com/watch?v=WZkZVCWxIXs – suku Jan 03 '16 at 15:02
-
I did the above steps. Default font is replaced successfully, but bold, italic, etc styles are not applied to the new font on device. – code_poetry Oct 21 '16 at 07:00
-
@Mishty try to get a bold, italics font version of the same font. And use setTypeface() and use these fonts – suku Dec 23 '16 at 14:58
-
1
You can achieve this in mainly 3 ways. one way would be to create a custom TextView and refer that everywhere, ie :
public class TypefacedTextView extends TextView {
public TypefacedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName);
setTypeface(typeface);
}
}
and inside View.xml
<packagename.TypefacedTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello"/>
Another way, is to use the powerful Calligraphy
library in github. see this
And finally, you can override the defaults with your own fonts, see this