Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

test: Reply
 
LinkBack Thread Tools
Old October 19th, 2012, 11:40 AM   #1 (permalink)
Member
Thread Author (OP)
 
Join Date: Nov 2010
Location: London
Posts: 210
 
Device(s): HTC Desire HD, Samsung Galaxy SII, Samsung Galaxy Nexus, ASUS Nexus 7
Carrier: Not Provided

Thanks: 16
Thanked 8 Times in 7 Posts
Default Using a custom font (Roboto) in my application?

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.

Advertisements
__________________
Twitter.

My Android apps: Link to play store.
RED_ is offline  
Reply With Quote
sponsored links
Old October 19th, 2012, 11:57 AM   #2 (permalink)
Junior Member
 
fiveHellions's Avatar
 
Join Date: Feb 2012
Gender: Male
Posts: 29
 
Device(s):
Carrier: Not Provided

Thanks: 1
Thanked 10 Times in 8 Posts
Default

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);
__________________
Android App Developer - fiveHellions development
Minimalist Shortcut - DeClutter Your Homescreen
Drunk Green Robots - Drinking Game
fiveHellions is offline  
Reply With Quote
Old October 19th, 2012, 12:08 PM   #3 (permalink)
Member
Thread Author (OP)
 
Join Date: Nov 2010
Location: London
Posts: 210
 
Device(s): HTC Desire HD, Samsung Galaxy SII, Samsung Galaxy Nexus, ASUS Nexus 7
Carrier: Not Provided

Thanks: 16
Thanked 8 Times in 7 Posts
Default

Quote:
Originally Posted by fiveHellions View Post
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.
RED_ is offline  
Reply With Quote
Old October 19th, 2012, 12:28 PM   #4 (permalink)
Junior Member
 
fiveHellions's Avatar
 
Join Date: Feb 2012
Gender: Male
Posts: 29
 
Device(s):
Carrier: Not Provided

Thanks: 1
Thanked 10 Times in 8 Posts
Default

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:

Quote:
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.
fiveHellions is offline  
Reply With Quote
Old October 19th, 2012, 01:00 PM   #5 (permalink)
Member
Thread Author (OP)
 
Join Date: Nov 2010
Location: London
Posts: 210
 
Device(s): HTC Desire HD, Samsung Galaxy SII, Samsung Galaxy Nexus, ASUS Nexus 7
Carrier: Not Provided

Thanks: 16
Thanked 8 Times in 7 Posts
Default

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.
RED_ is offline  
Reply With Quote
Reply
Tags
android, custom, eclipse, font change, fonts, install, roboto, textview


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 11:24 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.