• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Using a custom font (Roboto) in my application?

RED_

Well-Known Member
I recently decided to remake my UI, I had text built into my ImageViews which I was advised against, so I have now gone with TextViews, however I would like to carry on using the Roboto font. Is it possible to do so?

I have already added "Roboto-Light" to the assets/fonts folder but I'm looking for an easy way to implement it since I want some TextViews to have the font and some to be standard.

Is there a way I can do it in the XML? So I can select individual TextViews rather than the entire application?

Thanks for any advice. Appreciate it.
 
You can set it in code the following way. Not sure if there is an easy way to set it in xml.

TextView text = (TextView) findViewById(R.id.textview03);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Molot.otf");
text.setTypeface(tf);
 
You can set it in code the following way. Not sure if there is an easy way to set it in xml.

TextView text = (TextView) findViewById(R.id.textview03);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Molot.otf");
text.setTypeface(tf);

Is there a global way I can set it? So I don't have to keep adding code for every TextView? As in, I don't make the entire application one font, but I could it in a way that I can refer to it when I need to use it. The code you provided pulls the font from the folder, maybe I can attach it to a style? Then name the style in my XML.

When I think about it I guess it wouldn't be that bad.. just have quite a lot of TextViews. In fact some of my pages will have tonnes.
 
As far as I've seen you can only set the built in fonts as part of a style.

You can create a custom class the extends textview. Here is an example I found:

There is a fairly easy way to do this via XML. You just need to create your own widget that extends TextView.

First, create a file in res/values/attrs.xml with the following content:

Code:
<resources>
    <declare-styleable name="TypefacedTextView">
        <attr name="typeface" format="string" />
    </declare-styleable>
</resources>
After that, create your custom widget:
Code:
package your.package.widget;

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

}
As you can see, the code above will read a font inside the assets/ folder. For this example, I am assuming that there is a file called "custom.ttf" in the assets folder. At last, use the widget in the XMLs:
Code:
<your.package.widget.TypefacedTextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:your_namespace="http://schemas.android.com/apk/res/your.package"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Custom fonts in XML are easy"
    android:textColor="#FFF"
    android:textSize="14dip"
    your_namespace:typeface="custom.ttf" />
Note: you won't be able to see your custom font in Eclipse's layout editor. This is why I put the isInEditMode() check. But if you run your app, the custom font will work like a charm.
 
Thanks. I think I'll take the long route, do it by each ID like you posted the first time around. I looked into the other way and apparently it creates a memory leak. For now I think it's creates a bit more work. You have to use a hashmap or something to fix it.

Thanks again.
 
Back
Top Bottom