Setting text style of TextView programmatically

August 30, 2012

I noticed that many people have problem with setting style of the TextView dynamically. Myself I got stuck many times in the early days trying to do that. In the ideal world you would set the text style attribute in you layout XML definition like that:

<TextView
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:textStyle="bold"/>

There is a simple way to achieve the same result dynamically in your code by using TextView#setTypeface method. You need to pass and object of Typeface class, which will describe the font style for that TextView. So to achieve the same result as with the XML definition above you can do the following:

Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
textview.setTypeface(boldTypeface);

The first line will create the object form predefined style (in this case Typeface.BOLD, but there are many more predefined). Once we have an instance of typeface we can set it on the TextView. And that’s it our content will be displayed on the style we defined.

This is very brief post but I hope it will help somebody.