Monday, 5 March 2018

Custom Fonts in Android



I have already read some articles and searched on Google, but I failed to do it.



My problem is regarding the font-face.



In Android, there are only 4 attributes in "android:typeface": Normal, Sans, Serif, Monospace.



So what do I have to do to use "Verdana" in my application?




Please suggest me a correct way to use this font in my Android application.


Answer



This is a simple example... create a folder in the root of your project called assets/fonts/ then paste the TTF font file (in this case Verdana.ttf). Then, if you want to apply that font to, say a TextView, do the following:



import android.graphics.Typeface;

public class FontSampler extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

setContentView(R.layout.main);

TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(),
"fonts/Verdana.ttf");

tv.setTypeface(face);
}
}



This example was taken from the ComonsWare book (written by Mark Murphy). You can download the full example from GitHub.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...