TextView is one of the fundamental UI widgets in Android used to display text to users. It is a lightweight and flexible component that supports text formatting, style customizations, dynamic content updates, and accessibility features. Understanding TextView thoroughly is essential for building readable and accessible Android user interfaces.
This guide covers how to use TextView effectively with code examples and industry best practices.
What Is TextView?
TextView is an Android UI component that displays text strings to users. It’s commonly used for labels, headings, descriptions, and dynamic content such as status messages. It supports rich text formatting, font styles, colors, and text sizing, making it versatile for UI design.
Adding TextView in XML Layout
Here’s a basic TextView example in an XML layout (activity_main.xml):
<TextView
android:id="@+id/tvSample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, Android TextView!"
android:textSize="18sp"
android:textColor="@color/black"/>
This creates a TextView that displays a simple welcome message.
TextView Attributes Explained
TextView comes with several important attributes:
| Attribute | Purpose |
|---|---|
android:text | Text content displayed |
android:textSize | Font size |
android:textColor | Text color |
android:textStyle | Bold/Italic |
android:gravity | Text alignment inside view |
android:padding | Padding around text |
Styling TextView
Bold, Italic Text
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Styled Text"
android:textStyle="bold|italic"/>
Center Align Text
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Centered Text"/>
Dynamic Text Update in Java
You can update TextView text programmatically:
TextView tvSample = findViewById(R.id.tvSample);
tvSample.setText("Updated Text from Code");
Dynamic updates are common when displaying data from APIs, user input, or database queries.
Multi-Line Text and Line Breaks
To display multi-line text:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Line 1\nLine 2\nLine 3"/>
Use \n for line breaks or define text with formatted strings.
Text Appearance and Fonts
You can apply styles via textAppearance or custom fonts:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Font"
android:fontFamily="@font/roboto"/>
Custom fonts improve UI polish and brand consistency.
TextView with Ellipsize
For long text truncation:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="This is a very long text..."/>
This shows … when text overflows.
Clickable TextView
Make a TextView clickable in XML:
<TextView
android:id="@+id/tvClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tap Me"
android:clickable="true"
android:focusable="true"/>
Handle click in Java:
tvClick.setOnClickListener(v -> {
Toast.makeText(this, "TextView clicked!", Toast.LENGTH_SHORT).show();
});
Accessibility Considerations
For accessibility:
- Use descriptive text (
android:contentDescription) - Ensure contrast (textColor) meets visibility standards
- Support large fonts (
spinstead ofdp)
Example:
<TextView
android:contentDescription="User greeting message"
android:text="Welcome to the app"/>
Best Practices
From real Android engineering experience:
- Use
spfortextSizeto respect user settings - Avoid hardcoded text — use
strings.xml - Support accessibility with appropriate attributes
- Use styles/themes for consistent appearance
These practices improve maintainability and user experience.
Common Issues and Fixes
Text not visible?
✔ Ensure textColor is contrasting with the background.
Text overflow?
✔ Use ellipsize and maxLines.
Dynamic text not updating?
✔ Make sure you call setText() on the UI thread.
Summary
TextView is a fundamental UI component that you’ll use in nearly every Android app. Mastering its options — from dynamic updates to styling and accessibility — ensures your app’s text presentation is clear, responsive, and user-friendly.


