Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Roboto font in TextView - xml

I've found several posts regarding this topic, but all of this topics either sets the font with setTypeFace() method on a TextView object, or creating a custom class which sets the font to Roboto and extends TextView. As far as I know from API-Level 11(?) or something, we are able to set the TypeFace as a xml attribute, some how. Like this:

   <TextView
      android:id="@+id/profileHeader"
      android:layout_width="100dp"
      android:layout_height="100dp"
      android:typeface="roboto"
      android:text="Hello, world">
   </TextView>

What is the right way to do this? Is it possible to have a fallback if the application runs on a device lower than API level 11(?) something like:

 android:typeface="roboto|monospace|serif"
like image 874
Tobias Moe Thorstensen Avatar asked Mar 31 '13 19:03

Tobias Moe Thorstensen


People also ask

Is Roboto a default font?

Roboto is the default font on Android, and since 2013, other Google services such as Google Play, YouTube, Google Maps, and Google Images.

Is Roboto a universal font?

CONCLUSION. Intended to be a flexible digital font, Roboto exceeds its purpose. It is slowly evolving into a universal font that can serve many purposes, whether digital or print.


3 Answers

Take a look at the RobotoTextView project. Works down to Android 1.5, and you can set the typeface using XML attributes. It also includes other views like RobotoButton, RobotoCheckbox, etc.

like image 154
cnnr Avatar answered Sep 23 '22 01:09

cnnr


I don't see a way you can define an external typeface as a xml attribute. You should store the typeface in the assets and call:

tv.setTypeface( Typeface.createFromAsset( context.getAssets(), roboto.ttf ) );
like image 36
Vikram Gupta Avatar answered Sep 24 '22 01:09

Vikram Gupta


You can not set font directly from assets like this you have to do as follow in onCreate. This will make same thing what you want to do.

TextView tvTextView = (TextView) findViewById(R.id.textView1);
Typeface typeface = Typeface.createFromAsset(getAssets(),"Roboto.ttf");
tvTextView.setTypeface(typeface);

Hope it will help you out.:D

like image 1
sachin Avatar answered Sep 20 '22 01:09

sachin