Use Roboto font in app with minimum API level 14
Asked Answered
G

1

19

I have an app whose minimum API level is 14. Am I correct in thinking all compatible devices should have Roboto font installed as default? If I set a textView font to Roboto or Roboto Light it seems to default to the normal sans typeface.

Is there a way to use Roboto without including the Roboto font as an asset?

Gardie answered 31/1, 2013 at 17:9 Comment(3)
i think we can't do thisStabile
Why dont you add the .ttf to your asset and use it as a TypeFace? (If i understand your question correctly)Mandolin
I don't want to include the Roboto font as an asset as my app has a minimum API level of 14, so Roboto should be on compatible devices already.Gardie
L
58

Is there a way to use Roboto without including the Roboto font as an asset?

No there is no other way of doing this for API 11<.

I usually create a custom TextView for the Robot typeface:

public class TextView_Roboto extends TextView {

        public TextView_Roboto(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
                createFont();
        }

        public TextView_Roboto(Context context, AttributeSet attrs) {
                super(context, attrs);
                createFont();
        }

        public TextView_Roboto(Context context) {
                super(context);
                createFont();
        }

        public void createFont() {
                Typeface font = Typeface.createFromAsset(getContext().getAssets(), "robo_font.ttf");
                setTypeface(font);
        }
}

Now you can use it in your Layouts like this:

<com.my.package.TextView_Roboto>
  android:layout_width="..."
  android:layout_height="..."
  [...]
</com.my.package.TextView_Roboto>

Of course you can create a TextView layout. One for Pre HC, one for HC and above(You'll have to make use of the layout and layout-v11 folders). Now you can use the <include> tag to include the TextView in your Layout. You just have to do this use this then:

if (android.os.Build.VERSION.SDK_INT >= 11){
    TextView txt = (TextView) findViewById(R.id.myTxtView);
}
else{
    TextView_Roboto txt = (TextView_Roboto) findViewById(R.id.myTxtView);
}

Edit:

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
Luanaluanda answered 31/1, 2013 at 18:53 Comment(10)
Thanks Ahmad that clears it up nicely. I'll have to double check but I'm certain using a TextView with the typface set to Roboto in my styles.xml does not result in Roboto being used, instead the default sans font displays. My app has a minimum API level of 14 so I would have thought Roboto would be used?Gardie
There is no need to store the context in a mContext instance variable because there is the inherited getContext(). And why do you override setTypeface(...) when you are just calling the super one?Berns
@ZsoltSafrany Hmm don't know why I did that. Thanks for pointing out. I edited the answer.Luanaluanda
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 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. Sounds like Milo has found a variant of this problem. 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.Judiciary
Warning: Using the TextView_Roboto custom view as described above will more than likely result in a native heap memory leak (see here). A font cache should be implemented in tandem with the custom text view to prevent potential memory leaks - see here for details.Sluggard
@Luanaluanda Are there other equivalents of roboto ? Like Roboto regular, Roboto bold, Roboto medium ???Dachshund
@Dachshund Do you mean fonts that are built into the Android framework?Luanaluanda
@JamesB Thanks for the addition! I will edit the answer to include that.Luanaluanda
@Luanaluanda yes, all the ones that are mentioned here: developer.android.com/design/style/typography.html Is there a way to access them all through family-font tag ? Like you did show how to do for some in your editDachshund
@Dachshund I answered that a while ago here. It lists all possible combinations for the Roboto font.Luanaluanda

© 2022 - 2024 — McMap. All rights reserved.