I added several fonts to the application, for example open-sans-semibold . I created the assets folder and then threw the fonts into .ttf. Is it possible to mesh fonts not in software but in xml. Some twist elements I do not use from the activity and honestly do not see any reason to get them programmatically via id and give them the font like this:

 Typeface custom_font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/OpenSans-SemiBold.ttf"); txt_name.setTypeface(custom_font); 

Can this be done from xml?

1 answer 1

In API 26 appeared attribute android:fontFamily .

Create font in resources:

 <?xml version="1.0" encoding="utf-8"?> <font-family xmlns:android="http://schemas.android.com/apk/res/android"> <font android:fontStyle="normal" android:fontWeight="400" android:font="@font/myfont" /> </font-family> 

And apply:

 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="@font/myfont"/> 

Link

  • but you can not somehow from the folder created to connect? - Andrew Goroshko
  • Did not quite understand the question. Just put the ttf file in the res / font folder, for example, and further like in the example. - Enikeyschik
  • I now have a res / assets / fonts folder where I dropped the fonts, from here I was able to connect the font programmatically, and I thought it could be possible to connect them from this folder - Andrew Goroshko
  • offtopic: copied the code from the answer that I threw above) - Insider
  • @Insider From what answer? This is the code from the official documentation. - Enikeyschik