android:fontFamily="sans-serif" // roboto regular
android:fontFamily="sans-serif-light" // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-thin" // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium" // roboto medium (android 5.0)
Is FontFamily.SansSerif same as Roboto font in Jetpack Compose also?
The short answer is yes, referencing sans-serif is using Roboto, for Compose as well as for the old View-based UI system in Android.
In the context of Compose, it's important to understand that this is only true now and for Android.
Roboto is a sans-serif typeface, and by referencing sans-serif you're really referring to the current sans-serif type the system you're working on uses. It could potentially change in the future. And it could be different if you're running the code in web or desktop (e.g. Helvetica).
Related
I want to style some HTML with a condensed font. I want to use built-in fonts, not webfonts. For Apple devices, I can just write font-family: AvenirNextCondensed-Medium, but on Android, declarations like font-family: "Roboto Condensed", font-family: Roboto-Condensed or font-family: RobotoCondensed don't work, even though Roboto Condensed is preinstalled on Android.
I also tried specifying Roboto and using font-stretch: condensed. No dice.
Is there any way to write my CSS so that Android devices use their built-in Roboto Condensed font?
Answering my own question here:
You have to use the nonstandard sans-serif-condensed keyword, like so:
h1 { font-family: AvenirNextCondensed-Medium, HelveticaNeue, sans-serif-condensed, sans-serif; }
Note that sans-serif-condensed has to come before sans-serif and any other font names recognized by Android. For example, if you put Arial before sans-serif-condensed, Android will just use Arial (or actually its substitute for it).
For other versions of Roboto (Thin, Light, Medium, etc.), the font names listed in this StackOverflow answer should probably work.
I'm trying to do this
The important part of that code is here:
Notice this line of code where "fontAssetName" would be some font file (in my case it's DroidSans.ttf) which has to be in my assets folder.
final Typeface regular = Typeface.createFromAsset(context.getAssets(),
fontAssetName);
I want to do it without having to have a copy of ttf font file in my assets folder. This is because I don't want to have to deal with the copyright license of downloading a font. The font I want to use is DroidSans.ttf which should be provided in the android sdk as it is the default font. Is there any way I can access this font from the android sdk?
Android has built in fonts,
android:fontFamily="sans-serif" // roboto regular
android:fontFamily="sans-serif-light" // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-thin" // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium" // roboto medium (android 5.0)
If you want to use something different, you need to include the font yourself as you were doing.
Android doesn't include a ton of fonts like a desktop OS. The reason is that it bloats the distribution and creates a non-homogeneous look across apps.
Do I need to use Roboto font and put it to asset folder if my app will support only Android 4+ devices?
I would greatly appreciate for your help. Alex. P.S. Sorry for my English:)
You can use Roboto natively from Android 4.1+ like this:
android:fontFamily="sans-serif" // roboto regular
android:fontFamily="sans-serif-light" // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
For any version below that, you have to load the font yourself. Look at this answer I gave a while back for more information.
Answer
Ok. I think, I got it.
Samsung phones are by far the most popular Android phones. Whilst it's
true that all Samsung phones from 4.1 and up have Roboto, they also
have something called Samsung Sans, and if your user has set it as
their default font then the android:font-family (as #Ahmad says)
requests all return Samsung Sans, not Roboto. If you have fixed tight
layouts with no wriggle-room that can't stretch, Samsung Sans will
break them. There's no easy way round this. If you absolutely have to
have Roboto, you must package it as an asset and set it as the
typeface explicitly.
Thx to #Ahmad as well as #Kenton Price's comment here: https://stackoverflow.com/a/14633032/2553905
To add to Ahmad's excellent answer, you can swap out 'light' or 'condensed' to whichever style it is you want from the list in the Material Design guidelines here:
http://www.google.com/design/spec/style/typography.html#typography-roboto-noto
For example:
android:fontFamily="sans-serif-medium" // roboto medium
And so on.
This comment adds no more than Ahmad's, but just clears up that you are not limited to the 3 examples he gave, you can use any.
From documentation:
Ice Cream Sandwich introduced a new type family named Roboto, created
specifically for the requirements of UI and high-resolution screens.
Read more about Typography.
When setting custom fonts for a textview, I can only choose normal, bold or italic:
How can I set the style to be light instead of bold in the following example?
<TextViewWithCustomFont
...
android:textStyle="bold"/>
This is font specific. Not all fonts have a light, medium, thin attribute/style, but the default font should. You can use the default light font by using fontFamily: sans-serif-light or for thin, fontFamily: sans-serif-thin.
For custom fonts, you would need to include the light version of the font and use it.
Letter Spacing
As an adjacent solution to change a font's looks, you could use letterSpacing. This will change the distance between the letters within a TextView.
A positive number like 0.2 will add more of a gap, while negative like -0.1 will squish the letters together.
In Android 21+ you can programmatically call setLetterSpacing or in XML add letterSpacing.
You can only combine those three attributes :
normal
bold
italic
http://developer.android.com/reference/android/widget/TextView.html#attr_android:textStyle
So it can be :
normal
bold
italic
bold | italic
Previously, to make my app workable in Gingerbread device and above, I have to copy the Robotto font resource into asset folder. This is because Gingerbread doesn't come with Robotto font itself.
However, let say, I decide to deploy my app to Jelly Bean device only.
Do I still need to copy font resources into my asset folder manually? Can I use font resources from system itself? Is it something encourage-able? I was thinking, without supplying my own font files, I can make my app smaller.
This is the code to get TypeFace from asset folder.
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Regular.ttf");
If I want to get TypeFace directly from system itself, how?
The good news is, if you're supporting 4.1, it's dead simple. Check out this link and scroll down to fonts for the full details, but basically, you'll have three font families (Roboto, Roboto Light, RobotoCondensed) to choose from, and four styles for each (normal, bold, italic, bold italic).
In XML, you can just use the standard text attributes:
android:fontFamily="sans-serif"
android:fontFamily="sans-serif-light"
android:fontFamily="sans-serif-condensed"
android:textStyle="bold"
android:textStyle="italic"
android:textStyle="bold|italic"
Or programatically you can acquire them like so:
Typeface robotoLightItalic = Typeface.create("sans-serif-light", Typeface.ITALIC);