-2

In android Using setTypeface method we can set the font to the control of our wish programmatically, but i want to know is there a way we can avoid this and set the font in layout XML file itself?

I Just want to specify the path of the file & font should get updated automatically.

Naruto
  • 9,476
  • 37
  • 118
  • 201
  • Please read this post completely, there are several answers which can answer all your doubts. http://stackoverflow.com/questions/19691530/valid-values-for-androidfontfamily-and-what-they-map-to – Mohammed Atif Oct 11 '16 at 07:44

2 Answers2

1

You can use Calligraphy library where you can specify font in XML itself.

<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fontPath="fonts/Roboto-Bold.ttf"/>
Ashish Rathee
  • 449
  • 3
  • 16
  • HI, i was looking for the same, is it something like just include the gradle in my application and simply provide font path thats all?. will this work? – Naruto Oct 11 '16 at 07:54
  • Yes it will work...just take a look over documentation won't take more than 30 seconds :) – Ashish Rathee Oct 11 '16 at 07:55
  • ok, ill take look, if it works surely ill accept your answer, else i may need your help in this. thank you for help and support bro – Naruto Oct 11 '16 at 07:59
  • Heyy i put an answer but great answer. I used to do it myself using custom views. Does that really work ? – Amir Ziarati Oct 11 '16 at 08:05
  • HI, i tried, its not updating with new font, i just included the Lib in gradle & created my sample textview lile this `` it didnt work :-( – Naruto Oct 11 '16 at 08:12
  • 1
    Have you added CalligraphyConfig.initDefault(new CalligraphyConfig.Builder() .setDefaultFontPath("fonts/Roboto-RobotoRegular.ttf") .setFontAttrId(R.attr.fontPath) .build() in your Application class – Ashish Rathee Oct 11 '16 at 08:32
  • 1
    Add in Activity – Ashish Rathee Oct 11 '16 at 08:33
  • 1
    @Override protected void attachBaseContext(Context newBase) { super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); } – Ashish Rathee Oct 11 '16 at 08:33
  • Super, thank, your help saved my time, i have accepted answer. But one important thing, will it works fine for all OS?. how is calligraphy lib its stable right?. also you have any idea how it works internally?. Just for knowledge sake i'm asking, thanks – Naruto Oct 11 '16 at 10:03
  • Yes it should work fine. I haven't checked internal logic they implemented. If get time definitely check how it works... and post on my blog :) – Ashish Rathee Oct 11 '16 at 10:08
0

Yes. You need to extend the view you want to set its font in xml and then make an custom attribute (as enum) to be accessed from xml and call it for example "customFont".

Then in the extended view java code get the "customFont" value and change the font programaticaly.

Then you can use this custom view and set its font through xml ;)

Its possible for some limited font becuase you must define an enum for xml and see in java code wich font was selected and then set it in java code set the selected font.

Though you can set system font in xml without all these. And my explanation was for your custom fonts ;)

Amir Ziarati
  • 14,248
  • 11
  • 47
  • 52