How can I change font type in Android TextView?
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome!"
android:textSize="20sp" />
How can I change font type in Android TextView?
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome!"
android:textSize="20sp" />
You can use the android:typeface
attribute. For example:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome!"
android:typeface="sans"
android:textSize="20sp" />
The XML attribute only allows the values normal
, sans
, serif
, and monospace
. If you want to use a different Typeface
(perhaps a custom one that you ship with your app), you will have to do it in code by calling setTypeface()
for the view after the TextView is loaded.
To add to Ted Hopp's answer, if you are looking at a custom font for your TextView
, in your Activity
from where you reference the TextView
, use this code example:
Typeface blockFonts = Typeface.createFromAsset(getAssets(),"ROBOTO-MEDIUM_0.TTF");
TextView txtSampleTxt = (TextView) findViewById(R.id.your_textview_id);
txtSampleTxt.setTypeface(blockFonts);
Although, before you can use this, you will need to copy the font/s of your choice in the assets
folder.
You can also look at one of my answer on SO which has a couple of websites you can use to download fonts. They are:
You can programatically set the desire font by placing your font file (example.tff) style in Assets/fonts and then give your own string into fontpath variable
String fontPath="fonts/Unkempt-Regular.ttf";
TextView aboutus=(TextView)findViewById(R.id.aboutus);
Typeface type=Typeface.createFromAsset(getAssets(), fontPath);
aboutus.setTypeface(type);