Using a custom font (Roboto) in my application?


Last Updated:

  1. RED_

    RED_ Well-Known Member This Topic's Starter

    Joined:
    Nov 13, 2010
    Messages:
    209
    Likes Received:
    8
    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.
     

    Advertisement
  2. fiveHellions

    fiveHellions Active Member

    Joined:
    Feb 24, 2012
    Messages:
    29
    Likes Received:
    10
    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);
     
  3. RED_

    RED_ Well-Known Member This Topic's Starter

    Joined:
    Nov 13, 2010
    Messages:
    209
    Likes Received:
    8
    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.
     
  4. fiveHellions

    fiveHellions Active Member

    Joined:
    Feb 24, 2012
    Messages:
    29
    Likes Received:
    10
    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:

     
  5. RED_

    RED_ Well-Known Member This Topic's Starter

    Joined:
    Nov 13, 2010
    Messages:
    209
    Likes Received:
    8
    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.
     

Share This Page

Loading...