If I define my TextView
as:
<TextView style="@android:style/TextAppearance.DeviceDefault.Large" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" />
it is basically the same as doing:
<TextView android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" />
I know that style
is some kind of broader qualifier (i.e. one can't set all attributes in android:textAppearance
) but then it raises the question: why bother? Is there any advantage of using android:textAppearance
over style
?
The TextAttribute class defines attribute keys and attribute values used for text rendering. TextAttribute instances are used as attribute keys to identify attributes in classes handling text attributes. Other constants defined in this class can be used as attribute values.
Change Text Style of TextView to BOLD in XML Layout File textStyle attribute of TextView widget accepts on of these values: "bold" , "italic" or "normal" . To change the style to bold, you have to assign textStyle with "bold" .
Because the text appearance is checked first, any attributes defined either directly on a view or in a style will override the text appearance. There’s one other caveat to be aware of with TextAppearance which is that is supports a subset of styling attributes that TextView offers.
Essentially TextView first looks if you’ve supplied android:textAppearance, if so it loads that style and applies any properties it specifies. Later on, it then loads all attributes of the view (which remember, includes the style) and applies them.
Understanding how to declaratively style text on Android. When styling text in Android apps, TextView offers multiple attributes and different ways to apply them. You can set attributes directly in your layout, you can apply a style to a view, or a theme to a layout or perhaps set a text appearance.
In our demonstration, we dynamically changed the TextView style by tapping on it. To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
From Styles and Themes https://developer.android.com/guide/topics/ui/look-and-feel/themes#textappearance
One limitation with styles is that you can apply only one style to a View. In a TextView, however, you can also specify a TextAppearance attribute which functions similarly to a style
TextAppearance allows you to define text-specific styling while leaving a View’s style available for other uses. Note, however, that if you define any text attributes directly on the View or in a style, those values would override the TextAppearance values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With