android: set textColor with textAppearance attribute - android

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>

Related

When using Theme.MaterialComponents.Light.NoActionBar style, setting Button Background has no effect

In order to use Chip and ChipGroup, I set Application style extends Theme.MaterialComponents.Light.NoActionBar int manifests.xml, then I set Button "android:background" attr, but it does not effect! why? and what can I do?
This is my style:
<style name="AppBaseTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">#color/primary_material_light</item>
<item name="colorPrimaryDark">#color/header_color</item>
<item name="actionBarSize">48dp</item>
<item name="android:screenOrientation">portrait</item>
<!--<item name="buttonStyle">#style/AntButton</item>-->
<!--<item name="materialButtonStyle">#style/AntButton</item>-->
<!--<item name="android:button"></item>-->
<!--<item name="android:progressTint">#color/ffc000</item>-->
<item name="colorAccent">#color/ffc000</item>
</style>
<style name="AntButton" parent="android:Widget">
<item name="android:background">#drawable/abc_btn_default_mtrl_shape</item>
<item name="android:textAppearance">?android:attr/textAppearanceButton</item>
<item name="android:minHeight">48dip</item>
<item name="android:minWidth">88dip</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
<!--<item name="android:colorButtonNormal">#color/white</item>-->
</style>
I have tried to change buttonStyle and materialButtonStyle, but not effect too!
this is my layout XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/ll_popup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/white"
android:divider="#drawable/shape_divider_05dp"
android:orientation="vertical"
android:showDividers="beginning|middle">
<!--only can use backgroundTint to change background color-->
<Button
android:id="#+id/item_popupwindows_Photo"
android:layout_width="match_parent"
android:layout_height="55dp"
android:backgroundTint="#color/white"
android:text="Pictrue"
android:textColor="#color/c_666666"
android:textSize="16sp" />
<!--background not effect !!-->
<Button
android:id="#+id/item_popupwindows_cancel"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#color/white"
android:text="cancel"
android:textColor="#color/c_666666"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
this is result :
Because I have used Chip and ChipGroup in APP, I have to user theme extends MaterialComponents! do you know how to resolve it ?please tell me, thanks!
in this page Can't use android:background with button from the new material components, the author wants to change the default padding, but I want to change the backgroundDrawable, so, it does not work for me.
If you want a true Button, but one that you can modify like the framework Button (instead of the MaterialButton), then you can explicitly specify the framework version in your layout file. Replace this:
<Button
android:id="#+id/item_popupwindows_cancel"
... />
with this:
<android.widget.Button
android:id="#+id/item_popupwindows_cancel"
... />
This will give you what it says on the tin: an android.widget.Button, which should respond to styling the way you expect.
Similarly, you could use a <androidx.appcompat.widget.AppCompatButton> if you want support library features but not material components features.
In this particular case, the LinearLayout holding your second Button seems to have a white background color. That means you don't need to explicitly specify a white background for your Button; you can just let the LinearLayout's background show through.
This can be accomplished by using the "Text Button" style of MaterialButton:
style="#style/Widget.MaterialComponents.Button.TextButton"
Use this code for change the color
app:backgroundTint="#color/primaryColor"
You can also use ImageButton instead of Button from material components. android:background is working on ImageButton.
<ImageButton
android:id="#+id/item_popupwindows_cancel"
... />

Change the TextInputLayout outline color

I'm trying to customize a TextInputLayout with material style. I managed to set the focused state to the colors I want:
Using
<com.google.android.material.textfield.TextInputLayout
style="#style/LoginTextInputLayoutStyle"
android:theme="#style/LoginTextInputLayoutStyle"
android:textColorHint="#fff"
app:boxStrokeColor="#fff"
.....>
<EditText ...
where the style is:
<style name="LoginTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="colorAccent">#fff</item>
</style>
But when the textinput is not focused I get this look:
How can I change the color of the black line to be white too?
Use this style to apply border color and border width like this :
<style name="LoginTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="boxStrokeColor">#fff</item>
<item name="boxStrokeWidth">2dp</item>
</style>
get Additional details about styling from this link
Add below line in your colors.xml file that overrides default color for TextInputLayout
<color name="mtrl_textinput_default_box_stroke_color" tools:override="true">#fff</color>
As of version 1.1.0-alpha02 of the Material Components for Android it works to simply create a ColorStateList for these items. The procedure is as follows:
Create a new resource directory "color" in res and inside color add a color resource file named "text_input_box_stroke.xml" res/color/text_input_box_stroke.xml put something like the following:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#fcc" android:state_focused="true"/>
<item android:color="#cfc" android:state_hovered="true"/>
<item android:color="#ccf"/>
</selector>
Then in your styles.xml you would put:
<style name="LoginTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="boxStrokeColor">#color/text_input_box_stroke</item>
</style>
Finally indicate your style for the actual TextInputLayout:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/my_layout_id"
style="#style/LoginTextInputLayoutStyle"
...
As of Material Components Alpha 7 you simply create a color selector file as so:
colors/text_input_outline_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="#color/buttonDark"/>
<item android:state_hovered="true" android:color="#color/buttonDark"/>
<item android:state_focused="true" android:color="#color/buttonDark"/>
<item android:color="#color/buttonDark"/>
</selector>
For more context into how this is being set. Here is relevant source code:
ColorStateList boxStrokeColorStateList =
MaterialResources.getColorStateList(context, a, R.styleable.TextInputLayout_boxStrokeColor);
if (boxStrokeColorStateList != null && boxStrokeColorStateList.isStateful()) {
defaultStrokeColor = boxStrokeColorStateList.getDefaultColor();
disabledColor =
boxStrokeColorStateList.getColorForState(new int[] {-android.R.attr.state_enabled}, -1);
hoveredStrokeColor =
boxStrokeColorStateList.getColorForState(new int[] {android.R.attr.state_hovered}, -1);
focusedStrokeColor =
boxStrokeColorStateList.getColorForState(new int[] {android.R.attr.state_focused}, -1);
} else {
// If attribute boxStrokeColor is not a color state list but only a single value, its value
// will be applied to the box's focus state.
focusedStrokeColor =
a.getColor(R.styleable.TextInputLayout_boxStrokeColor, Color.TRANSPARENT);
defaultStrokeColor =
ContextCompat.getColor(context, R.color.mtrl_textinput_default_box_stroke_color);
disabledColor = ContextCompat.getColor(context, R.color.mtrl_textinput_disabled_color);
hoveredStrokeColor =
ContextCompat.getColor(context, R.color.mtrl_textinput_hovered_box_stroke_color);
}
From this list you can see that you want to ensure you are using a color selector with all states defined, or it will default back to another color.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#FFFFFF"/>
<item android:state_focused="false" android:color="#FFFFFF"/></selector>
create the color directory and inside that create a resource file
paste the above code in color directory xml file
and in text input layout style paste the below line
<item name="boxStrokeColor">#color/focus_tint_list</item>
First Remove from your TextInputLayout
<item name="boxStrokeColor">#color/YourColor</item>
Second, add a new color attribute
<color name="mtrl_textinput_default_box_stroke_color" tools:override="true" >YourColor</color>
Must write the same name mtrl_textinput_default_box_stroke_color do n't change
I'm creating dynamically my screen. I'm using TextInputLayout and create my dynamic edit text in TextInputLayout. If you want to give TextInputLayout to the border, do the following steps in order.
1- include Build.gradle;
implementation 'com.google.android.material:material:1.0.0'
2- in Kotlin code;
val textInputLayout = TextInputLayout(this)
textInputLayout.apply {
boxStrokeColor = Color.parseColor("#E68A06")
setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE)
setHintTextAppearance(R.style.ValidatableInputLayoutStyle_OutlineBox_HintInputLayoutStyle)
setBoxCornerRadii(16f, 16f, 16f, 16f)
setPadding(4, 0, 0, 0)
}
3- style.xml
<style name="ValidatableInputLayoutStyle.OutlineBox.HintInputLayoutStyle" parent="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="android:textColor">#color/colorPrimary</item>
<item name="android:textSize">14sp</item>
My Component image link
Matrial Edit Text
Step 1: Add library in build.gradle(Module App) module dependency section
implementation 'com.android.support:design:28.0.0-alpha1'
Step 2 : xml Code
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:boxStrokeColor="#0000FF"
app:boxStrokeWidth="2dp"
android:layout_gravity="center"
>
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/txtusername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/lable" />
</com.google.android.material.textfield.TextInputLayout>
I created a default config.
<style name="Widget.Design.TextInputLayout" parent="AppTheme">
<item name="hintTextAppearance">#style/AppTheme.TextFloatLabelAppearance</item>
<item name="errorTextAppearance">#style/AppTheme.TextErrorAppearance</item>
<item name="counterTextAppearance">#style/TextAppearance.Design.Counter</item>
<item name="counterOverflowTextAppearance">#style/TextAppearance.Design.Counter.Overflow</item>
</style>
<style name="AppTheme.TextFloatLabelAppearance" parent="TextAppearance.Design.Hint">
<!-- Floating label appearance here -->
<item name="android:textColor">#color/colorAccent</item>
<item name="android:textSize">20sp</item>
</style>
<style name="AppTheme.TextErrorAppearance" parent="TextAppearance.Design.Error">
<!-- Error message appearance here -->
<item name="android:textColor">#ff0000</item>
<item name="android:textSize">20sp</item>
</style>
Step 1. Use 1.2.0-alpha05 or later
implementation 'com.google.android.material:material:1.2.0-alpha05'
Step 2 - Important!. Make sure your app theme is or is a descendant of Theme.MaterialComponents. See here
Once that's set all attribute setting work as expected.
Step 3. Use the attribute setting from the specification
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/filledTextField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_text"
app:helperTextEnabled="true"
app:helperText="#string/helper_text"
app:counterEnabled="true"
app:counterMaxLength="20"
app:startIconContentDescription="#string/leading_icon_content_desc"
app:startIconDrawable="#drawable/baseline_favorite_24">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
Create a Theme and override "colorOnSurface" attr.
<style name="AppTheme.LoginScreenTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorOnSurface">#FFF</item>
</style>
Apply the theme to your login activity.
<activity
android:name=".login.ui.login.LoginActivity"
android:label="#string/title_activity_login"
android:launchMode="singleInstance"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.LoginScreenTheme"
android:windowSoftInputMode="adjustResize|stateHidden"/>
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
app:boxStrokeColor="#fff"
android:textColorHint="#fff"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
***app:boxStrokeColor="#fff"***
android:inputType="textPassword"
android:textColor="#fff"
/>
</com.google.android.material.textfield.TextInputLayout>
Step 1. add this line to your colors.xml file
<color name="mtrl_textinput_default_box_stroke_color">#4169E1</color>
Step 2. add this property in TextInputLayout
app:boxStrokeColor="#color/mtrl_textinput_default_box_stroke_color"
Summary
Steps to follow
1. Create ColorStateList for boxStrokeColor
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorSurface" android:state_focused="true" />
<item android:alpha="0.87" android:color="?attr/colorSurface" android:state_hovered="true" />
<item android:alpha="0.12" android:color="?attr/colorSurface" android:state_enabled="false" />
<item android:alpha="0.38" android:color="?attr/colorSurface" />
</selector>
2. Create ColorStateList for android:textColorHint
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="0.38" android:color="?attr/colorSurface" android:state_enabled="false" />
<item android:alpha="0.6" android:color="?attr/colorSurface" />
</selector>
3. Set view attributes - There are 3 ways that you can do this.
I. Using attribute set
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:textColorHint="#color/text_color_hint"
app:boxStrokeColor="#color/box_stroke_color"
app:hintTextColor="?attr/colorSurface">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
II. Using explicit style
Define custom style
<style name="CustomTextInputStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxStrokeColor">#color/box_stroke_color</item>
<item name="hintTextColor">?attr/colorSurface</item>
<item name="android:textColorHint">#color/text_color_hint</item>
</style>
Set style
<com.google.android.material.textfield.TextInputLayout
style="#style/CustomTextInputStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
III. Using default style attribute - set global style for TextInputLayout
<!-- Base application theme. -->
<style name="Theme.TestApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
...
<!-- Status bar color. -->
...
<!-- Customize your theme here. -->
<item name="textInputStyle">#style/CustomTextInputStyle</item>
</style>
Explanation
There are various ways to change TextInputLayout box stroke color and hint text color.
The responsible attribute for box outline color is boxStrokeColor.
First let's create ColorStateList in xml format. Create Android color resource directory and create new resource file named box_stroke_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorSurface" android:state_focused="true" />
<item android:alpha="0.87" android:color="?attr/colorSurface" android:state_hovered="true" />
<item android:alpha="0.12" android:color="?attr/colorSurface" android:state_enabled="false" />
<item android:alpha="0.38" android:color="?attr/colorSurface" />
</selector>
I referred resources of material design library. See how this is done in material design library https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/textfield/res/color/mtrl_outlined_stroke_color.xml
To change hint text color, we have to set two attributes,
hintTextColor (The color of the label when it is collapsed and the text field is active)
android:textColorHint (The color of the label in all other text field states, such as resting and disabled)
How do I know which attributes are required to change? I checked attributes defined in theme Widget.MaterialComponents.TextInputLayout.OutlinedBox. Look at parent theme if not defined in the child theme. https://github.com/material-components/material-components-android/blob/788866e4483621e2222f649e617ee95f7aa9caa6/lib/java/com/google/android/material/textfield/res/values/styles.xml#L88 (This may vary in master branch)
app:hintTextColor="?attr/colorSurface"
Note that hintTextColor is not stateful.
But android:textColorHint is stateful.
Let's create custom ColorStateList for android:textColorHint.
Referred this https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/textfield/res/color/mtrl_indicator_text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="0.38" android:color="?attr/colorSurface" android:state_enabled="false" />
<item android:alpha="0.6" android:color="?attr/colorSurface" />
</selector>
Note that I have used ?attr/colorSurface, because generally it's value is white in light theme and black in dark theme. You could use #android:color/white directly if you don't want dynamic color adjustments.
There are several ways that you can set box stroke color attribute value,
Attribute values are resolved using context.theme.obtainStyledAttributes().
If value is defined in multiple places, the following order determines which attribute value is ultimately applied.
AttributeSet - Value defined in layout xml file.
Style - Value defined in explicit style. (Retrieve it through theme.getExplicitStyle(AttributeSet))
defStyleAttr - Default style attribute, which is the third argument of view class constructor.
defStyleRes - Default style resource, which is the fourth argument of view class constructor.
Theme - If not defined in all of the above, theme attribute value is resolved.
Let's see them one by one
Using AttributeSet
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:textColorHint="#color/text_color_hint"
app:boxStrokeColor="#color/box_stroke_color"
app:hintTextColor="?attr/colorSurface">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
Using explicit style
Define custom style in styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTextInputStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxStrokeColor">#color/box_stroke_color</item>
<item name="hintTextColor">?attr/colorSurface</item>
<item name="android:textColorHint">#color/text_color_hint</item>
</style>
</resources>
Define explicit style for the widget
<com.google.android.material.textfield.TextInputLayout
style="#style/CustomTextInputStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
Using default style attribute - This is used to define global style for a view. How do you know which attribute is required to set? Just check default value of third argument of any view constructor. For material TextInputLayout this value is com.google.android.material.R.attr.textInputStyle.
<!-- Base application theme. -->
<style name="Theme.TestApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
...
<!-- Status bar color. -->
...
<!-- Customize your theme here. -->
<item name="textInputStyle">#style/CustomTextInputStyle</item>
</style>
Using default style resource - This is only applicable when creating widgets programmatically. If you check fourth argument of the constructor of any view except material design views, you can see defStyleRes parameter. If theme.obtainStyledAttributes() cannot resolve from the above ways, then it looks for attribute in default style resource. This is not applicable in material design library widgets, because this value is hard coded in those widgets and not exposed to change programmatically. (It is internally applied through theme.applyStyle())
Using app theme - This is not possible in material design widgets, because defStyleRes is hard coded in material design widgets and it takes precedence over app theme attributes.
<!-- Customize your theme here. -->
<item name="boxStrokeColor">#color/box_stroke_color</item>
<item name="hintTextColor">?attr/colorSurface</item>
<item name="android:textColorHint">#color/text_color_hint</item>
Above is only applicable in Android Sdk provided widgets.
---> First customize style
<style name="Widget.TextInputLayout.FilledBox" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
<item name="boxStrokeColor">?attr/colorSecondary</item>
<item name="hintTextColor">?attr/colorSecondary</item>
</style>
Second If you want to this style for all TextinputLayout in whole app.
so add this style to your parent theme
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryVariant">#color/colorPrimaryDark</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorSecondary">#color/colorSecondary</item>
<item name="colorSecondaryVariant">#color/colorSecondaryVariant</item>
<item name="android:textColorPrimary">#color/textColorPrimary</item>
<item name="textInputStyle">#style/Widget.TextInputLayout.FilledBox</item>
</style>
If you just want to add only for this particular input field
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.TextInputLayout.FilledBox"
.....>
<style name="VerifyTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="boxStrokeColor">#color/colorWhite</item>
<item name="boxStrokeWidth">2dp</item>
<item name="colorOnSurface">#color/colorPrimary</item>
<item name="colorPrimary">#color/colorWhite</item>
</style>
For the border color:
app:boxStrokeColor="#color/gray" //for border color
For the hint color:
app:hintTextColor="#color/puce" //for hint color
This worked for me:
<com.google.android.material.textfield.TextInputLayout
android:textAppearance="?attr/textAppearanceBody2"
style="#style/LoginTextInputLayoutStyle"
android:theme="#style/LoginTextInputLayoutStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/txtUserName"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:textColor="#FFFFFF"
android:textColorHint="#FFFFFF"
android:textColorHighlight="#FFFFFF"
android:shadowColor="#FFFFFF"
android:outlineAmbientShadowColor="#FFFFFF"
android:outlineSpotShadowColor="#FFFFFF"
android:hint="Email"
android:textSize="#dimen/textxhdpi"/>
</com.google.android.material.textfield.TextInputLayout>
In the Styles:
<resources
xmlns:app="http://schemas.android.com/apk/res-auto">
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
<item name="textAppearanceBody2">#style/TextAppearance.neurosales.Body2</item>
</style>
<style name="TextAppearance.neurosales.Body2" parent="TextAppearance.MaterialComponents.Body2">
<item name="colorAccent">#color/white</item>
<item name="android:textColorHint">#color/white</item>
<item name="colorControlNormal">#color/white</item>
<item name="colorControlActivated">#color/white</item>
<item name="colorControlHighlight">#color/white</item>
<item name="android:textColor">#color/white</item>
</style>
<style name="LoginTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="boxStrokeColor">#fff</item>
<item name="android:textColorHint">#fff</item>
<item name="app:hintTextColor">#fff</item>
</style>
In Color:
<color name="mtrl_textinput_default_box_stroke_color" tools:override="true">#fff</color>

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: How do you set default view attributes?

I have multiple buttons that all have the same textSize, layout_width, etc. and I do not want to copy and paste these attributes over and over as it makes for hard to edit code. In other words, I am looking to take many of these:
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/round_button_normal"
android:text="#string/button1"
android:textSize="50sp" />
And instead be able to use a preset button (where the layout_width, layout_height, layout_weight, background, and textSize are all set to the above values, by default):
<PresetButton
android:id="#+id/button1"
android:text="#string/button1" />
Just use styles.
Styles and Themes
Example:
In: res/values/styles.xml
<style name="Numbers">
<item name="android:inputType">number</item>
</style>
Use like this:
<EditText
style="#style/Numbers" />
You can use styles.xml to create a style, then you can apply that styling to each button.
http://developer.android.com/guide/topics/ui/themes.html
in your values -> style.xml file put the xml attribute
<style name="buttonConfirm" parent="#android:style/Widget.Button">
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">15dip</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
and in your layout call it by this xml
<Button
android:id="#+id/button1"
style="#style/buttonConfirm"
android:text="Button"
/>

TextColor defined in style being applied to TextView, but not Button?

I've defined some style resources that include TextAppearance with a defined TextColor. I then apply the styles to some TextViews and Buttons. All the styles come through with the TextView, but not the Button. For some reason, the textColor attribute is not showing up. Is this a bug, or am I missing something in the case of the Button?
Here is the style definition:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="TestApp">
</style>
<!-- Text Appearances -->
<style name="TestApp.TextAppearance">
<item name="android:typeface">sans</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">16px</item>
<item name="android:textColor">#6666FF</item>
</style>
<!-- Widget Styles -->
<style name="TestApp.Widget">
<item name="android:layout_margin">3sp</item>
</style>
<style name="TestApp.Widget.Label">
<item name="android:textAppearance">#style/TestApp.TextAppearance</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="TestApp.Widget.Switch">
<item name="android:textAppearance">#style/TestApp.TextAppearance</item>
<item name="android:layout_width">100px</item>
<item name="android:layout_height">100px</item>
</style>
</resources>
and here's the layout where I attempt to apply them:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
style="#style/TestApp.Widget.Label"
android:text="This is my label." />
<TextView
style="#style/TestApp.Widget.Label"
android:text="This is my disabled label."
android:enabled="false" />
<Button
style="#style/TestApp.Widget.Switch"
android:text="This is my switch." />
<Button
style="#style/TestApp.Widget.Switch"
android:text="This is my disabled switch."
android:enabled="false" />
</LinearLayout>
Apparently the way to achieve this is to override the textColor attribute in the button styling. Android does it this way in their Global Theme Styles:
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/styles.xml
I'd love to understand why this is the case.
For the case of Buttons, there are two ways to define text color through attributes: textColor and through a style defined by textAppearance.
The value set by textColor (which is set by the default style) will override any text color value set by the textAppearance style. Therefore you must set the textColor attribute to #null in one of two ways.
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" />
There appears to be a textAppearanceButton as well:
http://developer.android.com/reference/android/R.attr.html#textAppearanceButton
You may want to try that.

Categories

Resources