0

I cannot get a custom font to work in a Fragment in Android Studio version 1.5. Here is what I have so far in the java document.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    View view = inflater.inflate(R.layout.fragment_sponsors, container, false);
    Typeface myTypeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/COOPPBL.TTF");
    TextView tv = (TextView) view.findViewById(R.id.textview1);
    tv.setTypeface(myTypeface);

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

And this is what I have in the pertinent XML code:

<TextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/sponsors"
    android:textSize="35sp"
    />

The font does not change and the output remains the default type. I have already created an assets folder in the main project folder, with a "fonts" sub-folder which contains the file "COOPBL.TTF". What am I missing? I feel like it is just a silly error; I am very new to the platform.

Slug03
  • 3
  • 2

2 Answers2

1

try this,

return the view which you inflate and set the custom font to textview.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    View view = inflater.inflate(R.layout.fragment_sponsors, container, false);
    Typeface myTypeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/COOPPBL.TTF");
    TextView tv = (TextView) view.findViewById(R.id.textview1);
    tv.setTypeface(myTypeface);

    return view;
}
Pradeep Gupta
  • 1,770
  • 1
  • 9
  • 23
0

You can try this

@Override
public View onCreateView(
    LayoutInflater inflater, 
    ViewGroup container,
    Bundle savedInstanceState) {
       View v = inflater.inflate(R.layout.fragment_sponsors, container, false);

       TextView tv = (TextView) v.findViewById(R.id.textview1);

       Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/COOPPBL.TTF"");

       tv .setTypeface(font); 
       return v;
}
Pang
  • 9,564
  • 146
  • 81
  • 122