Android - set TextView TextStyle programmatically?

Jake Wilson picture Jake Wilson · Oct 27, 2011 · Viewed 250.9k times · Source

Is there a way to set the textStyle attribute of a TextView programmatically? There doesn't appear to be a setTextStyle() method.

To be clear, I am not talking about View / Widget styles! I am talking about the following:

<TextView
  android:id="@+id/my_text"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Hello World"
  android:textStyle="bold" />

Answer

Raz picture Raz · Oct 27, 2011
textview.setTypeface(Typeface.DEFAULT_BOLD);

setTypeface is the Attribute textStyle.

As Shankar V added, to preserve the previously set typeface attributes you can use:

textview.setTypeface(textview.getTypeface(), Typeface.BOLD);