BaRud
BaRud

Reputation: 3218

textview is not obeying resource fontfamily

I am using fonttype defined this way, and android studio's layout design view is showing the string/textview in the weathericon font.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryLight">

    <TextView
        android:id="@+id/tvFragThird"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:fontFamily="@font/weathericons"
        android:text="@string/textview"
        android:textSize="30sp" />
</RelativeLayout>

But,when I am running this piece of code:

public class ThirdFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_third, container, false);

        TextView tv = (TextView) v.findViewById(R.id.tvFragThird);
//        Typeface typeface = ResourcesCompat.getFont(getContext(), R.font.weathericons);
//        tv.setTypeface(typeface);
        tv.setText("&#xf0eb;");//getArguments().getString("msg"));

return v;
}

I am just getting the text in normal font. I have tried both defining the fontfamily in layout and typeface in java, and none of them are working.

what I am missing?

Upvotes: 2

Views: 918

Answers (1)

Arash
Arash

Reputation: 698

As it's mentioned here https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml, custom font using fontFamily, needs API >= 26.

So change TextView tag to android.support.v7.widget.AppCompatTextView tag (if you use API < 26).

I had this problem and i came across above link and did above change and my problem is solved and i hope, it solves yours (if it has not been solved yet) and others.

Update:

As RobCo commented, if you can change to AppCompatActivity, you don't need above change. It's mentioned here : https://developer.android.com/reference/android/support/v7/widget/AppCompatTextView

Upvotes: 1

Related Questions