TextView
textStyle
bold
italic
Android development

How to set TextView textStyle such as bold, italic

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In Android app development, the TextView widget is commonly used to display text. To enhance the appearance of text in a TextView, Android developers can adjust the text style to bold, italic, or a combination of both. This article explores the ways to set the textStyle for a TextView, focusing on the methods provided by the Android framework and XML attributes.

Setting TextView textStyle in XML

The most straightforward method to configure a TextView's text style is directly within the XML layout file. This approach is widely preferred because it separates style from logic, making the code cleaner and easier to maintain.

To set the textStyle, use the android:textStyle attribute. Here is an example:

xml
1<TextView
2    android:layout_width="wrap_content"
3    android:layout_height="wrap_content"
4    android:text="Hello, World!"
5    android:textStyle="bold|italic"
6/>

The android:textStyle attribute can take three options:

  • bold: Makes the text appear in bold.
  • italic: Renders the text in italic.
  • bold|italic: Combines bold and italic styles.

Technical Explanation

The property android:textStyle allows multiple styles to be applied by separating them with a pipe (|). Internally, this attribute utilizes a bitmask to represent various styles.

For example: To apply both bold and italic styles, the TextView combines them using the | operator. This is resolved by the Android resource compiler which subsequently interprets the bitmask and applies both styles.

Setting TextView textStyle Programmatically

In some situations, you may need to change the TextView style dynamically through Java or Kotlin code. Here's how you can do it:

Using Java

java
TextView textView = findViewById(R.id.textView);
textView.setTypeface(null, Typeface.BOLD_ITALIC);

Using Kotlin

kotlin
val textView = findViewById<TextView>(R.id.textView)
textView.setTypeface(null, Typeface.BOLD_ITALIC)

Technical Explanation

The setTypeface() method is used to change the style of the text. The method takes two parameters:

  • Typeface: A reference to a Typeface object (can be null for default).
  • style: An integer denoting the text style, such as Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC, or Typeface.BOLD_ITALIC.

Customizing Typeface

For greater customization beyond bold and italic, Android supports custom typefaces. This allows developers to load their own font assets and apply them to TextView objects.

Using XML

xml
1<TextView
2    android:layout_width="wrap_content"
3    android:layout_height="wrap_content"
4    android:text="Custom Font"
5    android:fontFamily="@font/custom_font"
6/>

Using Code

Assuming the font file (custom_font.ttf) has been added to the res/font directory, you can apply it programmatically:

Using Java

java
Typeface customFont = ResourcesCompat.getFont(context, R.font.custom_font);
textView.setTypeface(customFont);

Using Kotlin

kotlin
val customFont: Typeface? = ResourcesCompat.getFont(context, R.font.custom_font)
textView.typeface = customFont

Table Summary

Below is a table summarizing the different ways to set the text style in a TextView:

MethodApproachExample Usage
XML AttributeStaticandroid\:textStyle="bold | italic"
Java CodeDynamictextView.setTypeface(null, Typeface.BOLD_ITALIC)
Kotlin CodeDynamictextView.setTypeface(null, Typeface.BOLD_ITALIC)
Custom Font XMLStaticandroid:fontFamily="@font/custom_font"
Custom Font CodeDynamictextView.typeface = ResourcesCompat.getFont(context, R.font.custom_font)

Conclusion

Setting the textStyle for a TextView in Android is a simple process whether using XML attributes for static text styling or code for dynamic changes. For those requiring further customization, the Android framework offers support for custom fonts. By understanding these methods and their implementation, developers can create more engaging and visually appealing mobile applications.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.