Rahim
Rahim

Reputation: 928

Roboto font in my android app

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.

Upvotes: 5

Views: 20571

Answers (3)

samleighton87
samleighton87

Reputation: 579

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/

Upvotes: 1

Reno
Reno

Reputation: 33792

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);

Upvotes: 18

th30perator
th30perator

Reputation: 66

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

Upvotes: 1

Related Questions