I have created several styles for font in my app.
How can I add these styles to views? - 1) Using style attribute 2) Using textAppearance.
To use style is not an option because views may have other attributes (margins, paddings, min width, etc - I cant specify 2 styles for a view), so I need to specify text-related attributes separately, but android:textColor
doesnt work here:
styles.xml:
<style name="TextAppearance.baseText" parent="@android:style/TextAppearance">
<item name="android:textAppearance">?android:textAppearanceSmall</item>
<item name="android:typeface">sans</item>
<item name="android:textColor">@android:color/white</item>
<item name="android:textSize">15sp</item>
</style>
layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="65dp"
android:orientation="horizontal" >
<Button
android:id="@+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sometext"
android:textAppearance="@style/TextAppearance.baseText"/>
</RelativeLayout>
Why doesnt it work? How can I specify textcolor in textappearance?
Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.
Create and apply a style To create a new style or theme, open your project's res/values/styles. xml file. For each style you want to create, follow these steps: Add a <style> element with a name that uniquely identifies the style.
TextAppearance styles can be seen as the Android equivalent of Material Design type styles. For custom styles, we recommend two approaches to help separate concerns and create a single source of truth for type theming values in your app: Store all TextAppearance styles in a single res/values/type. xml file.
As Oderik has mentioned in the comments, the Button view has a default value for it's textColor
attribute. This textColor
value overrides whatever value is set through the textAppearance
attribute. Therefore you have two options:
Set the textColor to @null in the style:
<style name="Application.Button">
<item name="android:textAppearance">@style/Application.Text.Medium.White</item>
<item name="android:textColor">@null</item>
</style>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Application.Button" />
Set the textColor to @null in the Button XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Application.Button"
android:textColor="@null" />
It works. Your text color is white - the same as default color. Put there any other color like <item name="android:textColor">#AA6699</item>
and you will see difference.
Second thing is that you are trying to set text appearance in text appearance - doen's make sense. If you want to set small letters do this using parent parent="@android:style/TextAppearance.Small
and delete line <item name="android:textAppearance">?android:textAppearanceSmall</item>
Third thing is that you want to have small letters and put additional textSize - doesn't make sense.
Try this, works for me:
<style name="BaseText" parent="@android:style/TextAppearance.Small">
<item name="android:typeface">sans</item>
<item name="android:textColor">#AA7755</item>
</style>
If you want to set everything in style:
<style name="BaseText" parent="@android:style/TextAppearance.Large">
<item name="android:typeface">sans</item>
<item name="android:textColor">#AA7755</item>
</style>
<style name="BaseText.Margins">
<item name="android:layout_margin">10dip</item>
</style>
Where BaseText.Margins inherits from BaseText. Then you can just use:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
style="@style/BaseText.Margins" />
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