Kushal Sharma
Kushal Sharma

Reputation: 5973

Android fontFamily sans-serif-light

I need some clarification regarding sans-serif-light font for android. The following line of code

<item name="android:fontFamily">sans-serif-light</item>

in custom style say <style name="custom_style"/> gives an error if minSdkVersion is less than 16.

but the same property directly on a textview

android:fontFamily="sans-serif-light" 

gives a warning if the minSdkVersion is less than 16.

Q1 What is the difference between the 2 approaches?

Q2 If i set the property directly in textview (2nd way) will the application be compatible with sdk version below 16 with the default font?

(I have only one device with android 4.4.4 and it works great on it with both ways)

Upvotes: 0

Views: 6720

Answers (2)

Confuse
Confuse

Reputation: 5786

As blackbelt said, it must be due to difference in lint settings.

San-serif light font will NOT work in devices below API 16. To fix this, you need to upload a custom font (.ttf file) file in asset.


Here is how to do it -

First copy the san-serif light font .ttf file to your asset folder which you can download from here. The file will contain many fonts but you just need san-serif light file.

Then in your code, do this -

Typeface light = Typeface.createFromAsset(getContext().getAssets(), "san-serif-light.ttf");
//Make sure that the font file's name is actually san-serif-light

Then you can set this to a textview this way -

textview.setTypeface(light);

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157447

If i set the property directly in textview (2nd way) will the application be compatible with sdk version below 16 with the default font?

The behaviour should not change even if you set the font family in the styles. The difference is probably due to your Lint's settings.

Upvotes: 1

Related Questions