android:lineSpacingMultiplier doesn't work in android:textAppearance - android

I'm trying to add android:lineSpacingMultiplier in my textAppearance style (android:textAppearance), and it's not working. Am I doing something wrong?
TextAppearance style:
<style name="TextAppearance.Body1" parent="TextAppearance.AppCompat.Body1">
<item name="android:lineSpacingMultiplier">1.25</item>
</style>
Use of style:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is body 1\nThis is body 1"
android:textAppearance="TextAppearance.Body1"
/>

For whatever reason android:lineSpacingMultiplier doesn't work as an item within your textAppearance. You'll either have to set it directly as a TextView attribute (using android:lineSpacingMultiplier), or create a regular style which "wraps" setting the textAppearance and lineSpacingMultiplier
Create a style
<style name="TextBody1">
<item name="android:textAppearance">#style/TextAppearance.AppCompat.Body1</item>
<item name="android:lineSpacingMultiplier">1.25</item>
</style>
or
<style name="TextBody1" parent="TextAppearance.AppCompat.Body1">
<item name="android:lineSpacingMultiplier">1.25</item>
</style>
and then apply via style instead of android:textAppearance
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is body 1\nThis is body 1"
style="#style/TextBody1"
/>

lineSpacingMultiplier as well as lineSpacingExtra you can apply only in style.
But as an alternative, you can use lineHeight attribute, which can be applied to the MaterialTextView using textAppearance.
Interesting note, that if you use a fully Material theme (not Bridge) all your TextView will auto-inflate to MaterialTextView, otherwise, you will need to specify <com.google.android.material.textview in your xml.

I was able to solve the issue by simply applying the style to the TextView itself instead of the textAppearance (similar to the accepted answer, but with a bit less code).
Here is a sample:
Style:
<style name="TextBody1" parent="TextAppearance.AppCompat.Body1">
<item name="android:lineSpacingMultiplier">1.25</item>
</style>
or simpler if you don't care about the parent:
<style name="TextBody1">
<item name="android:lineSpacingMultiplier">1.25</item>
</style>
Then in your View:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is body 1\nThis is body 1"
style="#style/TextBody1"
/>
I am not sure why applying android:textAppearance to a TextView with a given style that defines android:lineSpacingMultiplier does not work (I would speculate that it may be due to the fact that the line spacing is on the style View itself instead of the textAppearance of the View) but this is a bit simpler than the accepted answer if you don't care about the parent.

Related

Color of the text is not changing when style including textColor is applied to textAppearance of textView

I want to reduce my xml code repetition. So I made some standard styles for text in textView. We can apply styles under 'style' attribute as well as 'android:textAppearance' attribute in textView.
Below are some styles I made for text appearance-
<style name="Grey">
<item name="android:textColor"> #333333 </item>
</style>
<style name="CodeFont" parent="#android:style/TextAppearance.Medium">
<item name="android:textColor"> #00FF00 </item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">20sp</item>
</style>
When I apply these styles under 'textAppearance' attribute the color of the text is not changing in none of the above styles. It's working under 'style' attribute of textView.
//textColor not working
<TextView
android:id="#+id/profile_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Full Name"
android:textAppearance="#style/CodeFont"/>
//textColor working
<TextView
android:id="#+id/profile_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Full Name"
style="#style/CodeFont"/>
I want them to work under 'textAppearance' attribute so that I can apply some other style under 'style' attribute. And according to android documentation we can apply textColor styles under 'textAppearance' attribute.
Please suggest some solution to this.
Thanks
Try setting the text color in your widget as null like this:
<TextView
android:id="#+id/profile_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Full Name"
android:textColor="#null" //add this line
android:textAppearance="#style/CodeFont"/>
Also, I think you should try to Invalidate cache and Restart Android Studio. Import and linking issues can be solved like this sometimes.
This snippet works for me
<style name="fonts" parent="TextAppearance.AppCompat">
<item name="android:textColor">#245546</item>
<item name="android:textSize">30dp</item>
</style>
and textview is
<TextView
android:id="#+id/sample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome to stackoverflow"
android:textAppearance="#style/fonts"/>
Sometimes if you don't give android:textColor in styles you can have this issue that text color won't appear in your TextView, So the solution is to just give completely <item name="android:textColor">#color/yourColor</item> in styles rather than this <item name="textColor">#color/yourColor</item>.. your problem would be resolved :)

Android: textColor ignored in android:textAppearance of the EditText

Is there any reason why is textColor property being ignored when it is set via custom textAppearance style?
<style name="EditTextAppearance" parent="#android:style/TextAppearance.Widget.EditText">
<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textSize">16sp</item>
<item name="android:textColor">#color/blue</item> <!-- IGNORED -->
</style>
Setting style in XML:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#style/EditTextAppearance"
/>
For some reason, default theme control color is not overridden by this style.
The only way how I am able to set color is by setting textColor property in EditText (but this is not what I want):
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#style/EditTextAppearance"
android:textColor="#color/blue"
/>
Note, custom textAppearance with textColor created for TextView works without problem.
I tried to replace EditText by android.support.v7.widget.AppCompatEditText or android.support.design.widget.TextInputEditText but the result was the same. Still not working. So the problem is not in EditText implementation.
I found question with the same problem Why is textColor in android:textAppearance ignored?. Unfortunately, without answer.
Somewhere along the line, the wrong/default value for textColor is being picked up and applied. You can force the android:textColor that you define in android:textAppearance to be used by setting android:textColor="#null" in your XML for EditText.
android:textColor ignored in android:textAppearance of the EditText because android:editTextColor attribute always overrides text color of the EditText (don ask me why, its about crazy droid).
But all works fine if you add this attribute to your theme:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:editTextColor">#null</item>
</style>
Still don't know why is textAppearance ignoring textColor when style is created as described in question, but I think I have found another way how to set textColor via style, so somebody might find it useful.
I was playing with EditText colors so I've created custom theme and tried to set editTextColor property, which surprisingly worked:
<!-- consider to extend application theme over default "Base.V7.Widget.AppCompat.EditText" to achieve consistent look and feel -->
<style name="EditTextTheme" parent="Base.V7.Widget.AppCompat.EditText">
<item name="colorControlNormal">#color...</item> <!-- underline color -->
<item name="colorControlActivated">#color...</item> <!-- underline color on focus -->
<item name="editTextColor">#color/blue</item> <!-- ACCEPTED -->
</style>
Setting theme:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#style/EditTextAppearance"
android:theme="#style/EditTextTheme"
/>
At this moment you have custom "#style/EditTextAppearance" and "#style/EditTextTheme". The good thing is, that you can merge "#style/EditTextAppearance" into the "#style/EditTextTheme" as follows:
<style name="EditTextTheme" parent="Base.V7.Widget.AppCompat.EditText">
...
<item name="editTextColor">#color/blue</item>
<item name="editTextStyle">#style/EditTextAppearance</item>
</style>
This way, you just have to set only theme in your EditText:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/EditTextTheme"
/>
Current solution is almost perfect, but it would be better, if textColor will be set in textAppearance style, so I've tried to remove editTextColor from theme and replace it by textColor property set in textAppearance style #style/EditTextAppearance.
<style name="EditTextAppearance" parent="#android:style/TextAppearance.Widget.EditText">
<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textSize">16sp</item>
<item name="android:textColor">#color/blue</item> <!-- APPLIED if appearance is part of the theme -->
</style>
<style name="EditTextTheme" parent="Base.V7.Widget.AppCompat.EditText">
<item name="colorControlNormal">#color...</item>
<item name="colorControlActivated">#color...</item>
<item name="editTextStyle">#style/EditTextAppearance</item>
</style>
This way textColor contained in textAppearance is finally applied!

Material Raised Button with Default Background

I am trying to create a button whose text color is the colorAccent and whose background color is the default background color.
It looks like this:
I have tried default styles, but non of them have worked. Currently I have something like this but doesn't work:
styles.xml :
<style name="AppTheme.ButtonRaised" parent="Widget.AppCompat.Button.Colored">
<item name="colorButtonNormal">#android:color/transparent</item>
<item name="android:textColor">#color/colorAccent</item>
</style>
And layout:
<Button
app:theme="#style/AppTheme.ButtonRaised"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="date" />
And I am getting something like this:
I am not getting any effect of style or theme here. I have also tried with style tag, but it doesn't work.
apply theme with a style like this
<style name="ThemeButtonBlue" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorAccent">#color/colorPrimary</item>
<item name="colorButtonNormal">#android:color/transparent</item>
<item name="colorControlNormal">#color/white</item>
<item name="colorControlActivated">#color/colorPrimaryDark</item>
<item name="colorControlHighlight">#color/colorPrimary</item>
</style>
I am also struggling to do this effect. I could achieve something similar, but I don't know if its "material design approved".
I set the style to their button borderless colored and the elevation to 2dp because on material design they say that "Raised buttons have a default elevation of 2dp". But they don't give you xml layout examples which is annoying -_-
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
style="#style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:elevation="2dp"
android:text="Button"/>
Keep in mind that the elevation tag does not show a shadow pre Lollipop devices.

AppCompat v22: Set default textcolor for TextView

I would like to set a default textcolor in my AppTheme, which should be black (not the default Material Design dark grey). The textColor should be overriden by setting a custom style via android:textAppearance-attribute on a UI-element (e.g. TextView).
This is my current setup:
I use AppCompat-Library in my project:
compile 'com.android.support:appcompat-v7:22.2.0'
I defined the following theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
<item name="android:textColorPrimary">#color/black</item> <!-- All TextViews should have this color as default text color -->
</style>
which is set in the AndroidManifest.xml:
<application
android:name=".core.BaseApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
Furthermore I defined some styles to change the textAppearance of some TextViews:
<style name="App.Heading" parent="android:TextAppearance.Widget.TextView">
<item name="android:textSize">16sp</item>
<item name="android:textColor">#color/red</item>
</style>
<style name="App.Heading.Highlighted" parent="android:TextAppearance.Widget.TextView">
<item name="android:textSize">16sp</item>
<item name="android:textColor">#color/blue</item>
</style>
Now I have a xml-layout with some TextViews. Some of them should have the default textAppearance (in fact a black text color as defined as android:textColorPrimary in my theme). Some should apply a custom textappearance from my defined styles:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="CUSTOM: App.Heading"
android:textAppearance="#style/App.Heading" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="CUSTOM: App.Heading.Highlighted"
android:textAppearance="#style/App.Heading.Highlighted" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Normal Textview with no style"/>
The first two TextView apply my defined textAppearance, which is fine. But the last TextView has a dark grey textcolor (Material Design default?) and not a black one. If I set the attribute:
<item name="android:textColor">#color/black</item>
in my AppTheme, all TextViews have a black text color. The textcolor defined in my styles (e.g. App.Heading) is no longer recognized.
So my question is: How can I set a default textColor for my TextViews, which could be overridden by setting a textAppearance?
It is not easy to find the way because there are three styleable android:textColor attributes, textColorPrimary textColorSecondary and textColorTertiary, and a lot of attributes for base styles android:textAppearance as small medium etc (now deprecated?).
Anyway, about TextView without style, it seems that it refers as default to the android:textAppearanceSmall styleable attribute of which value is style TextAppearance.AppCompat.Small that overrides through Base.TextAppearance.AppCompat.Small the android:textColor attribute with ?android:attr/textColorTertiary value.
Then overriding it like the following will work:
<item name="android:textColorTertiary">#color/black</item>
If I remember well, you cannot set the color of the text using the textAppearance. You need to use a style or a theme to achieve this.
Please define following code in style.xml files which are available in v11 ,v14 and v21 folder.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textColor">#android:color/red</item>
</style>
and use this style for every textview.

android: set textColor with textAppearance attribute

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?
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 want to use android:TextAppearance instead of style: to set your android:textColor, you need to set android:textColor=#null on your TextView.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:textAppearance="#style/TextAppearance.AppCompat.Medium.MySubheader"
android:textColor="#null"
tools:text="Section" />
Then it will inherit correctly from your android:TextAppearance
<style name="TextAppearance.AppCompat.Medium.MySubheader">
<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textSize">16sp</item>
<item name="android:textColor">#color/black_54</item>
</style>

Categories

Resources