Monday, March 5, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...