How to specify, for example. Roboto-Medium or Roboto-Black in styles.xml

Messages like this How to change fontFamily in a TextView in Android suggests that the Roboto font options that you can specify in styles.xml in Android 4.2 come down to the following:

  • Normal
  • Italics
  • Fatty
  • Bold italics
  • Light
  • Light tilt
  • Thin
  • Thin oblique
  • Condensed regular
  • Compressed Italics
  • Compressed fat
  • Compressed bold

This eliminates the possibility of style TextViews using, for example. Roboto-Medium or Roboto-Black fonts.

But why does Google add wide system fonts that cannot be used to style your TextViews? Of course, there must be some way to specify all Roboto fonts from styles.xml (i.e. DO NOT add inline fonts as assets and create your own text elements in the code), but how?

+58
android android-fonts android-typeface
Jan 09 '14 at
source share
4 answers

On Android 5.0, you can install Roboto using sans-serif-medium .

This solution, taken from Google iosched 2014 , uses sans-serif on Android pre-v21:

values ​​/ styles.xml

 <style name="MyStyle"> <item name="android:fontFamily">@string/font_fontFamily_medium</item> </style> 

values ​​/ fonts.xml

 <string name="font_fontFamily_medium">sans-serif</string> 

values-V21 / fonts.xml

 <string name="font_fontFamily_medium">sans-serif-medium</string> 
+88
Oct 24 '14 at 9:47
source share

NEW SOLUTION

After Google I / O '17, you can create several font families in the res / font folder

 <?xml version="1.0" encoding="utf-8"?> <font-family xmlns:app="http://schemas.android.com/apk/res-auto"> <font app:font="@font/roboto_light" app:fontStyle="normal" app:fontWeight="300" /> <font app:font="@font/roboto_bold" app:fontStyle="normal" app:fontWeight="700" /> </font-family> 

and you can use wherever you want

  <style name="AppTheme.DefaultTextView" parent="android:style/Widget.TextView"> <item name="android:textColor">@color/colorBlack</item> <item name="android:fontFamily">@font/font_family_roboto</item> </style> 

font folder font family

OLD RESPONSE

Thanks for Jacob Ericsson

original answer

 android:fontFamily="sans-serif" // roboto regular android:fontFamily="sans-serif-light" // roboto light android:fontFamily="sans-serif-condensed" // roboto condensed android:fontFamily="sans-serif-black" // roboto black android:fontFamily="sans-serif-thin" // roboto thin (android 4.2) android:fontFamily="sans-serif-medium" // roboto medium (android 5.0) 

stylish:

 <style name="AppTheme.DefaultTextView" parent="android:style/Widget.TextView"> <item name="android:textSize">@dimen/text_view_text_size</item> <item name="android:textColor">@color/black</item> <item name="android:fontFamily">sans-serif-light</item> <item name="android:textStyle">normal</item> </style> 

in the application topic:

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/gray</item> <item name="android:textViewStyle">@style/AppTheme.DefaultTextView</item> </style> 
+32
Mar 21 '17 at 12:55
source share

I suggest you use custom libraries like Android-RobotoTextView or Calligraphy . With one of them, you can set the view font in xml with an attribute so that you can insert it in styles.xml. And they work with a previous version of Android than 4.0

+4
Aug 18 '14 at 9:48
source share

Support Library 26 introduced using fonts in XML and is backward compatible with Android API 14.

To add fonts as resources, follow these steps in Android Studio:

  • Right-click the res folder and choose New> Android Resource Directory. The New Resource Directory window will appear.

  • In the Resource Type list, select a font, and then click OK. Note. The name of the resource directory must be a font.

Figure 1. Adding a font resource directory

  1. Add font files to the font folder. The folder structure below generates R.font.dancing_script , R.font.lobster and R.font.typo_graphica .

Figure 2. Adding font files to the resource directory

  1. Double-click the font file to view the font of the file in the editor.

Figure 3. Preview font file

To set the font for the TextView , in the layout XML file, set the fontFamily attribute to the font file that you want to access.

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

Android Studio Layout Preview allows you to view the font installed in TextView.

Figure 4. Font preview in layout preview

To add a font to a style, open styles.xml and set the fontFamily attribute to the font file that you want to access.

 <style name="customfontstyle" parent="@android:style/TextAppearance.Small"> <item name="android:fontFamily">@font/lobster</item> 

If you don't want to bind fonts in your application, you can always see Downloadable Fonts

+3
Dec 14 '17 at 3:52
source share



All Articles