5

Hi i am writing an android app. And I want the roboto font in it irrespective of the version of the phone. Is there a way to do it?

Thanks, Rahim.

Rahim
  • 918
  • 1
  • 12
  • 23

3 Answers3

18

Yeah why not, you can get the Roboto font :

Android ICS typography

Lets say you want to change the font of a text view :

   Typeface tf = Typeface.createFromAsset(getAssets(),
            "fonts/Roboto-Black.ttf");
    TextView tv = (TextView) findViewById(R.id.FontTextView);
    tv.setTypeface(tf);
Reno
  • 33,594
  • 11
  • 89
  • 102
  • 6
    Is there a way to set this typeface property in the XML itself? Or more simply, in the AndroidManifest.xml itself, to use across the whole app? – Rahim Jan 19 '12 at 18:23
  • 1
    I didn't find any way to do this either, so I ended up with this http://stackoverflow.com/questions/9797872/use-roboto-font-for-earlier-devices/10563831#10563831 – Arnaud May 12 '12 at 12:42
1

To set the font in XML is moderately more effort but has the advantage of being able to preview the font inside the Eclipse ADT‘s graphical layout tab of the XML layout editor. Again, first include your custom font .ttf file in the your application’s assets folder.

Create a custom textview class:

public class TypefacedTextView extends TextView
{
 public TypefacedTextView(Context context, AttributeSet attrs)
 {
  super(context, attrs);

   // Typeface.createFromAsset doesn't work in the layout editor. Skipping ...
   if (isInEditMode())
   {
    return;
   }

   TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.TypefacedTextView);
   String fontName = styledAttrs.getString(R.styleable.TypefacedTextView_typeface);
   styledAttrs.recycle();

   if (fontName != null)
    {
     Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName);
     setTypeface(typeface);
    }
   }
  }

Now to include this custom TypefacedTextView in your XML Layouts simply add your XML namespace attribute below the Android XML namespace attribute:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:your_namespace="http://schemas.android.com/apk/res/com.example.app"
... />

And use your TypefacedTextView as you would a normal TextView in XML but with your own custom tag, remembering to set your font:

<com.example.app.TypefacedTextView
  android:id="@+id/list_item_entry_title"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center_vertical"
  android:minHeight="48dp"
  android:textColor="#FF787878"
  your_namespace:typeface="Roboto-Regular.ttf" />

See my blog post for more info: http://polwarthlimited.com/2013/05/android-typefaces-including-a-custom-font/

samleighton87
  • 559
  • 1
  • 5
  • 13
1

Try this link http://www.barebonescoder.com/2010/05/android-development-using-custom-fonts/

Set the typeface property of the control you are targeting to serif... and for the font file I recommend using TTF, it has worked for me in the past

Also try these links

http://techdroid.kbeanie.com/2011/04/using-custom-fonts-on-android.html

Android - Using Custom Font

Community
  • 1
  • 1