-1

I am relatively new to Android Studio and I am starting to explore more the Android Navigation Drawer activities. I have been trying to change the font type of a textview from a fragment class and I just can't. I have tried many different solutions available here and none of them worked. In the picture, you can see my latest attempt.

In this image you can see the last code I tried to change the font from the fragment class

4 Answers4

0

Create one Class and name it whatever you want, that class will extend TextView, and that class will load the font. And further you can use that font in your xml itself. Like this:

public class CustomEditTextGEFlowBook extends android.support.v7.widget.AppCompatEditText {

    public CustomEditTextGEFlowBook(Context context) {
        super(context);if(!isInEditMode())
        setFont(context);
    }

    public CustomEditTextGEFlowBook(Context context, AttributeSet attrs) {
        super(context, attrs);if(!isInEditMode())
        setFont(context);
    }

    public CustomEditTextGEFlowBook(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);if(!isInEditMode())
        setFont(context);
    }

    private void setFont(Context context) {
        Typeface font = Typeface.createFromAsset(getContext().getAssets(),
                "GOTHAM-BOOK.OTF");
        setTypeface(font);
    }
}

Now from your xml, use your TextView or EditText like this. .

<com.packageName.CustomEditTextGEFlowBook
    android:id="@+id/radioOthers"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@null"
    android:hint="Mention the Issue"
    android:maxLength="50" />
Aman Shekhar
  • 2,719
  • 1
  • 18
  • 29
0

Verify whether font relative path is properly mentioned or not.

Amit Thakkar
  • 94
  • 1
  • 9
0

I just checked your code, It looks okay to me. One change though, change your code from

return inflater.inflate(R.layout.fragment_main, container, false);  

to

return rootView;

Also make sure you have provided the correct Font name

Clint Paul
  • 313
  • 2
  • 6
  • 18
0

I use this Library and it works superb on all devices

Step:-

1) Add this line in your gradle compile 'com.github.balrampandey19:FontOnText:0.0.1'

If this lines are missing add it in your gradle:-

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

2) Add your font in asset folder like this

3) Then replace your view in xml with

<com.balram.library.FotTextView
                    android:id="@+id/vno_tv"
                    .
                    .
                    android:textSize="14sp"
                    app:font="regular.ttf" />

this line is important to set custom font you want app:font="regular.ttf"

You can do same for Buttons Edittext

OR

If you want to use same "Font" through whole application you can follow this Guide here

Akshay Katariya
  • 1,464
  • 9
  • 20