Android is installed with a Roboto font in bold, italics, regular, ... (something like a custom font family)

I know that ho sets up a custom font programmatically inside an Android application. Is there a way to download a font for a custom font (assets) and the Android framework will use the correct file based on bold, italic and so on?

For example, now I'm trying to set the Roboto font for some TextView

 Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf"); textView.setTypeface(typeface); 

It is working fine. But since I am installing the TextView inside the bold bold xml layout, the text is not bold

 <TextView android:id="@+id/my_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="50dp" android:textStyle="bold" android:gravity="center" android:text="@string/my_text" android:textColor="@color/my_foreground" android:textSize="24dp" /> 

How to load a font from assets that will work?

 textView.setTypeface(typeface, Typeface.BOLD); 

There is only one "font family" inside my resource

 Roboto-Black.ttf Roboto-BlackItalic.ttf Roboto-Bold.ttf Roboto-BoldCondensed.ttf Roboto-BoldCondensedItalic.ttf Roboto-BoldItalic.ttf Roboto-Condensed.ttf Roboto-CondensedItalic.ttf Roboto-Italic.ttf Roboto-Light.ttf Roboto-LightItalic.ttf Roboto-Medium.ttf Roboto-MediumItalic.ttf Roboto-Regular.ttf Roboto-Thin.ttf Roboto-ThinItalic.ttf 

How to load all these fonts within one font / family?

+4
source share
2 answers

Not sure if it’s a good idea to post a link, but anyway ... Here is my blog post on this topic, it has a class that completely solves this problem. http://anton.averin.pro/2012/09/12/how-to-use-android-roboto-font-in-honeycomb-and-earlier-versions/

+3
source

I don’t know how to handle different font variants of the same font as one family, but fonts have large file sizes, so you probably won’t want to import all these fonts into your application. Instead, you can only use the medium font, and then set bold and italic attributes for it.

For example, if you have android: textStyle = "bold" set in your XML layout, you can do this in your code to maintain a bold style:

 Typeface currentTypeFace = textView.getTypeface(); if (currentTypeFace != null && currentTypeFace.getStyle() == Typeface.BOLD) { textView.setTypeface(tf, Typeface.BOLD); } else { textView.setTypeface(tf); } 
+2
source

Source: https://habr.com/ru/post/1399367/


All Articles