DEV Community

10x learner
10x learner

Posted on • Originally published at 10xlearner.com on

Quick Tip – How to make a TextView bold or italic ?

Hello ! I’m Xavier Jouvenot and in this small post, I am going to explain how to change TextView into a bold or italic.

Self promotion: You can find other articles on Android development on my website 😉

Bold or Italic by default

To transform a TextView text into a bold or an italic text, there are several solutions.And the first one we are going to see is how to make it bold or italic by default.

To do so, we are going to modify the TextView XML definition, and add one attribute to it.This attribute is android:textStyle and can be set to bold, italic, normal or bold|italic, in case you want you text to be both bold and italic.

Here is how it look like:

<TextView
    <!--Some attributes-->

    android:textStyle="bold"

    <!--Some other attributes --> >;
Enter fullscreen mode Exit fullscreen mode

If not specified, this attribute is set to normal.

Modify the text style dynamically

Specifying the attribute in the XML is great!But if you want to modify the style of a TextView while the program is running, then, you must use some Java code and the method setTypeface.

Here is how it looks like:

TextView tv = findViewById(R.id.my_text_view_id);
tv.setTypeface(tv.getTypeface(), Typeface.ITALIC);
Enter fullscreen mode Exit fullscreen mode

In this sample of code, you can replace ITALIC by BOLD, NORMAL or BOLD_ITALIC depending on what you want to do with your TextView.


Thank you all for reading this article,And until my next article, have an splendid day 😉

Interesting links

Top comments (0)