I need to apply custom fonts in my android project once for whole project. helps will be appreciated.
-
create a theme and put your font code in that – Ankur Aggarwal Dec 28 '16 at 12:23
-
first you can extend the Application class, createFontFromAsset() and use reflection to asign it to all components or you can use this thrid party library https://github.com/chrisjenx/Calligraphy, its a shame android doesnt allow a decent way to apply custom fonts for app except for system ones – marcos E. Dec 28 '16 at 12:24
-
By using [Android-DataBinding](https://developer.android.com/topic/libraries/data-binding/index.html), it will be easy for you to set more than one custom fonts, check reference link on [androidgig.com](http://www.androidgig.com/setting-custom-font-through-xml-with-databinding/). – Ravi Dec 28 '16 at 12:32
-
Responses appreciated. Was helpfull – Ahmad Dec 28 '16 at 12:45
1 Answers
THis is the answer which was posted by me on this Link. How to use custom font in Android Studio Here we have a better way to apply fonts on EditTexts and TextViews on android at once and apply it in whole project.
First of All you need to make fonts folder. Here are Steps.
1: Go to the (project folder) Then app>src>main
2: Create folders named 'assets/fonts' into the main folder.
3: Put your fonts into the fonts folder. Here I Have 'MavenPro-Regular.ttf'
Here are the Steps for applying custom fonts on EditText and using this approach you can apply fonts on every input.
1 : Create a Class MyEditText (your preferred name ...)
2 : which extends EditText
3 : Apply your font
Here is code Example;
public class MyEditText extends EditText {
public MyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyEditText(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/MavenPro-Regular.ttf");
setTypeface(tf);
}
}
}
And in Here is the code how to use it.
MyEditText editText = (MyEditText) findViewById(R.id.editText);
editText.setText("Hello");
Or in Your xml File
<MyEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#fff"
android:textSize="16dp"
android:id="@+id/editText"
/>

- 1
- 1

- 909
- 10
- 18