Material 3 MaterialTextView
Overview
MaterialTextView is a derivative of AppCompatTextView that displays text with enhanced line height support.
Material 3 MaterialTextView, using com.google.android.material.textview.MaterialTextView
, auto-inflates from TextView
in Theme.Material3.*
themes. It supports line height styling via TextAppearance
, making it ideal for consistent typography. This guide covers implementation, accessibility, and theming.
Getting Started
- Add Material Components for Android library dependency
- Use
TextView
withTheme.Material3.*
for auto-inflation toMaterialTextView
- Alternatively, specify
com.google.android.material.textview.MaterialTextView
directly - Apply
TextAppearance
for typography and line height
Accessibility
- Set
android:contentDescription
if text is insufficient for TalkBack - Ensure text contrast (4.5:1 for small text, 3:1 for large text)
Behavior and Configuration
- Configure text, appearance, and line height via attributes or programmatically
- Use
TextAppearance
for consistent typography
val textView = findViewById<com.google.android.material.textview.MaterialTextView>(R.id.text_view_id)
textView.text = "Hello, Material!"
textView.setTextAppearance(R.style.TextAppearance_Material3_BodyLarge)
MaterialTextView Anatomy
Component | Description | Behavior |
---|---|---|
Text Container | Displays text with Material typography, colorOnSurface | Renders styled text, supports line height |
MaterialTextView Example
- Displays text with customizable typography and line height
- Auto-inflates in Material 3 themes
Example
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/text_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textAppearance="?attr/textAppearanceBodyLarge"
android:contentDescription="@string/hello_desc"/>
</LinearLayout>
val textView = findViewById<com.google.android.material.textview.MaterialTextView>(R.id.text_view_id)
textView.text = "Welcome to Material 3"
textView.setTextAppearance(R.style.TextAppearance_Material3_BodyLarge)
Attributes and Usage
Element | Attribute | Related Method(s) | Default Value | Usage Description |
---|---|---|---|---|
Text | android:text |
setText , getText |
null | Sets displayed text |
Appearance | android:textAppearance |
setTextAppearance |
null | Sets typography style |
Line Height | android:lineHeight |
N/A (set via TextAppearance) | Inherited from TextAppearance | Sets line height via TextAppearance |
Content Description | android:contentDescription |
setContentDescription |
null | Sets accessibility description |
Theming MaterialTextView
- Customize typography and colors via
TextAppearance
- Use
res/values/styles.xml
for theming
Example
<style name="Theme.App" parent="Theme.Material3.*">
<item name="textAppearanceBodyLarge">@style/TextAppearance.App.BodyLarge</item>
</style>
<style name="TextAppearance.App.BodyLarge" parent="TextAppearance.Material3.BodyLarge">
<item name="android:textColor">@color/shrine_pink_900</item>
<item name="android:lineHeight">28dp</item>
</style>
<com.google.android.material.textview.MaterialTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textAppearance="@style/TextAppearance.App.BodyLarge"
android:contentDescription="@string/hello_desc"/>
Material Design Documentation
- Adheres to Material Design guidelines for text display
- Covers typography, line height, and accessibility
- Supports Material 3 theming
- Refer to Material Design documentation for full details
FAQ
What is MaterialTextView?
- TextView derivative with line height support
How does it differ from TextView?
- Auto-inflates in Material 3 themes, supports
TextAppearance
line height
When to use MaterialTextView?
- Use for consistent Material 3 typography
How to make it accessible?
- Set
contentDescription
if needed; ensure text contrast
Can I customize its appearance?
- Yes, theme via
TextAppearance
styles
How to add the Material 3 library?
- Include Material Components for Android library
Are there updates for the latest standards?
- Reflects latest Material 3 standards for MaterialTextView
Tags:
MDC Android