Skip to content Skip to sidebar Skip to footer

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.

Solution 1:

Yeah why not, you can get the Roboto font :

Android ICS typography

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

Typefacetf= Typeface.createFromAsset(getAssets(),
            "fonts/Roboto-Black.ttf");
    TextViewtv= (TextView) findViewById(R.id.FontTextView);
    tv.setTypeface(tf);

Solution 2:

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

Solution 3:

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:

publicclassTypefacedTextViewextendsTextView
{
 publicTypefacedTextView(Context context, AttributeSet attrs)
 {
  super(context, attrs);

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

   TypedArraystyledAttrs= context.obtainStyledAttributes(attrs, R.styleable.TypefacedTextView);
   StringfontName= styledAttrs.getString(R.styleable.TypefacedTextView_typeface);
   styledAttrs.recycle();

   if (fontName != null)
    {
     Typefacetypeface= 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:

<LinearLayoutxmlns: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/

Post a Comment for "Roboto Font In My Android App"