Custom Font on Android - android

I want to use a custom font in an Android app but using custom fonts seems to be a pain?!
I can setup a custom font programmatically by adding it to each TextView -> lots of boilerplate code. I can create a new class CustomTextView extends TextView and add setTypeface in the constructor -> no xml context aware help for attributes anymore.
Is there another way to setup a custom font? By XML, by style, by theme?
Thanks in advance!
P.S. Is there a possibility to set an underline by XML?

use this code for change font textview..
TextView newfont;
newfont=(TextView) findViewById(R.id.textView1);
Typeface font=Typeface.createFromAsset(getAssets(), "fonts/DejaVuSerif-Italic.ttf");
newfont.setTypeface(font);
newfont.setText("This is the new font Text");
download DejaVuSerif-Italic.ttf file from web and put in fonts folder in assets folder.

You can include custom fonts under assets/fonts and then using the code from
https://github.com/browep/AndroidCustomFontWidgets/
you can specify the font in your XML, e.g.
<com.github.browep.customfonts.view.FontableTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is in a custom font"
app:font="MyFont-Bold.otf" />
The code supports FontableTextView and FontableButton, but it's quite easy to extend to support other widget types if you require.

Related

for custom fonts is it necessary to add ActionBarActivity for Typeface?

I was reading about Custom Fonts in Android, But i have Confusion that for using Typeface(for getting your custom font from assets) is it possible to add any other activity type than ActionBarActivity().
someone please clarify my doubt.
No, There is no relationship your tutorial just demoed it under an activity which extends ActionBarActivity.
Right click on that > new Directory name it as fonts > copy your fonts to that
Then if you have a TextView that you need to apply your font
TextView myTextView=(TextView)findViewById(R.id.text_view);
Typeface typeFace=Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/your_cutom_font_name.ttf");
myTextView.setTypeface(typeFace);

Setting font in Android

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.
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"/>
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 ;)

How can i use clocktopia font which is already there in my Android Device?

I have a clocktopia font on my android device and it's only on some applications.
How do I use that particular font programmatically?
Thank you.
First, put the .ttf (or other font file) in your /assets directory.
Then, set the font face of your textview with the following code.
TextView textView = (TextView) findViewById(R.id.myTextView);
Typeface font = Typeface.createFromFile("path-to-file");
textView.setTypeface(font);
Unfortunately you cannot use custom typefaces in XML (so you also cannot define a custom theme with your font).
Custom fonts and XML layouts (Android)
You can use the TextViewPlus class provider here to load the typeface from xml but I don't know of a way to use a theme to do this automatically.

Android - Fonts in Application

I have added an external font in /assets directory, and manually doing setFacetype(font).
Isn't there a general way to set the whole application to use a specific font if you have added it external? Or do you have to use Android's selected fonts in order to achieve this?
You cannot use your custom fonts through to whole application in a general way.
You cannot set your custom fonts through xml files.
You have to use the Typeface functions in your code to use your custom fonts within your application.
tv=(TextView)findViewById(res);
Typeface font = Typeface.createFromAsset(this.getAssets(), "MYFONT.TTF");
tv.setTypeface(font);
This also how to use it in a textview.
For whole application go to Using a custom typeface in Android.
and go to Manish Singla answer
Typeface mTypeface = Typeface.createFromAsset(getAssets(), "YOUR FONT NAME");
textview.setTypeface(mTypeface, Typeface.NORMAL);

is there some easy way to include custom fonts in xml field declaration?

I included in created fonts folder in /assets and added some files .otf and .ttf. Is possible to create style and attach one of those fonts to some TextViews. I now to do this from code
like
Typeface tf2 = Typeface.createFromAsset(getAssets(),
"fonts/BPreplayBold.otf");
tv2 = (TextView) findViewById(R.id.key2);
tv2.setTypeface(tf2);
but is there some easier way to do this in xml, to include custom fonts ?
You can create your own TextView
class TypedTextView() extends TextView {
public TypedTextView(...) {
super(...);
this.setTypeFace(...)
}
And then use this widget in your XML layout
<com.exemple.com.TypedTextView>Hello with nice font</com.exemple.com.TypedTextView>
If you do things properly, I suppose you can get/set the font thanks to a custom XML attribute.
The only way to use custom fonts is through the source code.

Categories

Resources