Android Material Button - #drawable specs not recognized - android

New to AS. Trying to use MaterialButton, overriding default background with #drawable, but it's not working.
Environment
Android Studio: 4.0
Dependencies:
implementation "androidx.appcompat:appcompat:$dependency_version_appcompat"
implementation "com.google.android.material:material:$dependency_version_material"
Drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shape="rectangle">
<solid
app:backgroundTint ="?attr/ColorBackgroundAppBar">
</solid>
<stroke
android:width="1px"
android:color="?attr/ColorBorderDark">
</stroke>
</shape>
Layout
<com.google.android.material.button.MaterialButton
android:id="#+id/common_appbar_btn_lang_mode"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:background = "#drawable/common_appbar_btn_background"
android:elevation="#dimen/common_elevation_height_btn"
... />
With the above, I just get the default MaterialButton background.
If I set backgroundTint to #null
<com.google.android.material.button.MaterialButton
android:id="#+id/common_appbar_btn_lang_mode"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
app:backgroundTint="#null"
android:background = "#drawable/common_appbar_btn_background"
android:elevation="#dimen/common_elevation_height_btn"
... />
With the above, the drawable specs are recognized, but if I change the theme style, that doesn't work.
These are the only settings that work:
<com.google.android.material.button.MaterialButton
android:id="#+id/common_appbar_btn_lang_mode"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:backgroundTint = "?attr/ColorBackgroundAppBar"
app:strokeColor="?attr/ColorBackgroundAppBar"
app:strokeWidth="0px"
android:elevation="#dimen/common_elevation_height_none"
android:fontFamily="#font/font_family_kaiti"
android:text="简"
android:textAlignment="center"
android:textColor="?attr/ColorTextPrimary"
android:textStyle="normal"
android:textFontWeight="700"
android:textSize="24dp" />
Am I doing sth wrong...or is MaterialButton not quite there yet? I can live with the last solution, but it increases maintenance...

Define you app theme using Theme.MaterialComponents.DayNight:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryVariant">#color/colorPrimaryLight</item>
<item name="colorSecondary">#color/colorSecondary</item>
<!-- .... -->
</style>
Then, if you need, define the colors for the light and dark mode:
Example: res\values\colors.xml:
<color name="colorPrimary">.....</color>
In res\values-night\colors.xml folders define the same color:
<color name="colorPrimary">.....</color>
Finally define a style for your button (you don't need 2 different styles for dark and light mode):
<style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">#color/....</item>
<item name="strokeColor">#color/....</item>
<item name="strokeWidth">#dimen/....</item>
</style>
Last step assign the style to the Button in the layout.
<com.google.android.material.button.MaterialButton
style="#style/Widget.App.Button"
.../>
or globally in your app theme:
<item name="materialButtonStyle">#style/Widget.App.Button</item>
For your Button since you are not using a gradient you can use the standard attributes provided by the MaterialButton.
If you prefer to use the android:background attribute with a custom drawable you can use:
<style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">#null</item>
<item name="android:background">#drawable/.....</item>
</style>
Then inside the drawable just use the colors or attributes defined in the app theme and in the res\values\colors.xml and res\values-night\colors.xml folders.

Related

Android button background is taking the primary color

I've this issue, I don't know where it come from, I've created this buttons with custom background, but the background color talking the primary color and cannot change it unless change the primary color.
<Button
android:id="#+id/btn_teacher"
style="#style/normalText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/_16sdp"
android:background="#drawable/btn_rounded_stroke"
android:text="#string/txt_teacher" />
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="bg_color">#FFD7A4</color>
</resources>
I have many buttons with different colors, so i can't change the primary color
here is my drawable background
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape android:shape="rectangle" >
<size android:height="#dimen/_48sdp" />
<corners android:radius="#dimen/_24sdp" />
<stroke android:width="#dimen/_1sdp" android:color="#59CECE" />
<solid android:color="#android:color/white"/>
</shape>
</item>
</layer-list>
I'm using new material design by google
implementation "com.google.android.material:material:1.3.0-alpha02"
How can i override this color?
Since you are using a Theme.MaterialComponents.* your Button is replaced at runtime by a MaterialButton.
Currently the backgroundTint is still the default MaterialButton style.
It means that if you are using a custom android:background, you have to make sure to null out backgroundTint to avoid that the custom background doesn't get tinted with the attr/colorPrimary defined in your theme.
You have to add app:backgroundTint="#null":
<Button
app:backgroundTint="#null"
android:background="#drawable/.."
In any case you don't need a custom background (btn_rounded_stroke) in your case. You are just using a custom background only to define rounded corners. This feature is provided by default by the MaterialButton, then just use the cornerRadius attribute.
Use the standard MaterialButton:
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="48dp"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
app:strokeColor="#59CECE"
app:cornerRadius="24dp"
When Material theme is used then Button view is mapped to MaterialButtton.
There is one more way to setup the button background in this case.
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar"
...
<item name="materialButtonStyle">#style/ColoredButton</item>
</style>
<style name="ColoredButton" parent="Widget.MaterialComponents.Button.TextButton">
<item name="backgroundTint">#color/customButtonColor</item>
</style>
What worked for me
First create btn_style in your styles.xml or themes.xml
<style name="btn_style" parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">#null</item>
</style>
then in you layout apply the style and use the color or drawable that you want
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:background="#color/Red"
style="#style/btn_style"
android:text="Button"/>
Try this .
Your Button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_margin="5dp"
android:background="#drawable/round_btn"
android:padding="20dp"
android:text="My Text" />
Here is your round_btn drawable:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#008577" />
<corners android:radius="20dp" />
</shape>
In your Gradle just replace this line
implementation "com.google.android.material:material:1.3.0-alpha02"
with this
implementation 'com.google.android.material:material:1.2.1'
All you need to do is simply go to your themes.xml file and change the style name to:
style name="Theme.AnimatedLogin"
parent="Theme.AppCompat.Light.NoActionBar"
in Light theme mode and to:
style name="Theme.AnimatedLogin"
parent="Theme.AppCompat.DayNight.NoActionBar"
in Dark theme mode in your theme.xml(night) file and then you can customize your button and change its colour.

app:cornerRadius doesn't work for MaterialButton

<com.google.android.material.button.MaterialButton
android:id="#+id/button_tour"
app:cornerRadius="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:layout_gravity="center_horizontal"
android:text="Take a tour"/>
I added a material button in my android project, when i try to set round corner using app:cornerRadius, i find it doesn't work at all.
Just use material theme .change parent of AppTheme in styles to some material theme likeTheme.MaterialComponents.Light.NoActionBar.because material components require a material theme.
<style name="AppTheme.Clinic" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Try this:
add this style for outline material button
<style name="materialButtonOutlinedStyle" parent="#style/Widget.MaterialComponents.Button.OutlinedButton">
<item name="cornerRadius">10dp</item>
</style>
then add this style to your button:
<com.google.android.material.button.MaterialButton
android:id="#+id/button_tour"
style="#style/Widget.MaterialComponents.CardView"
app:cornerRadius="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:layout_gravity="center_horizontal"
android:text="Take a tour"/>
Create a new drawable source such as follows
<shape android:shape="rectangle">
<corners android:radius="20dp" />
</shape>
and set it as background to your MaterialButton

Android - spotShadowAlpha only for linearlayout

I am using the elevation attribute for my linearlayout, but the shadow is to bright. I need a darker shadow only for the linear layout.
I added android:spotShadowAlpha to my styles.xml. It worked, but not only for the linear layout. Every View has a darker shadow.
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:ambientShadowAlpha">0</item>
<item name="android:spotShadowAlpha">0.55 </item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
linearlayout in activity_main.xml
<LinearLayout
android:id="#+id/linl"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_marginStart="55dp"
android:layout_marginTop="208dp"
android:layout_marginEnd="56dp"
android:layout_marginBottom="209dp"
android:background="#drawable/custom_buttom"
android:elevation="70dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</LinearLayout>
Background #drawable/custom_buttom
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#FFFFFF"/>
<corners android:radius="999dp"/>
</shape>
DISCLAIMER: ONLY WORKS CORRECTLY FOR API 28 AND ABOVE
I know this question is kind of old, but i ran into the same problem. This guy has a pretty good solution. Essentially you set android:ambientShadowAlpha and android:spotShadowAlpha to 1. then you can use SetOutlineAmbientShadowColor and SetOutlineSpotShadowColor using a value that contains an alpha value to set it on an item by item basis. For everything else these act as the default shadow colors with system default alpha values:
<color name="defaultOutlineAmbientShadowColor">#0A000000</color> <color name="defaultOutlineSpotShadowColor">#30000000</color>
These are set in styles.xml:
<item name="android:ambientShadowAlpha">1</item> <item name="android:outlineAmbientShadowColor">#color/defaultOutlineAmbientShadowColor</item> <item name="android:spotShadowAlpha">1</item> <item name="android:outlineSpotShadowColor">#color/defaultOutlineSpotShadowColor</item>
android:ambientShadowAlpha and android:spotShadowAlpha theme attributes values are used at pretty low level, so there is no way to set specific values to these attributes for particular view of hierarchy (even using theme overlay approach).
It is possible to set custom values statically per activity basis:
create custom theme derived from your app theme and override android:ambientShadowAlpha and android:spotShadowAlpha in it:
<style name="Theme.YourApp" parent="...">
...
</style>
<style name="Theme.YourApp.StrongShadows">
<item name="android:ambientShadowAlpha">1</item>
<item name="android:spotShadowAlpha">1</item>
</style>
In manifest set this theme for your activity:
<application
android:theme="#style/Theme.YourApp">
<activity android:name=".ActivityWithDefaultShadows" />
<activity
android:name=".ActivityWithStrongShadows"
android:theme="#style/Theme.YourApp.StrongShadows" />
</application>
The best effort you can make at the moment to have better shadow customization is the approach that #ottermatic suggested in his answer (which of course works only for API level 28 and above). I'll describe it more accuratly:
in themes.xml file in values resources folder define base app theme and app theme derived from based without mentioned attributes:
<style name="Base.Theme.YourApp" parent="...">
...
</style>
<style name="Theme.YourApp" parent="Base.Theme.YourApp"/>
in themes.xml file in values-v28 override app theme for api versions 28 and above:
<style name="Theme.YourApp" parent="Base.Theme.YourApp">
<item name="android:ambientShadowAlpha">1</item>
<item name="android:spotShadowAlpha">1</item>
<item name="android:outlineAmbientShadowColor">#color/defaultOutlineAmbientShadowColor</item>
<item name="android:outlineSpotShadowColor">#color/defaultOutlineSpotShadowColor</item>
</style>
We set android:ambientShadowAlpha and android:spotShadowAlpha to 1 so they do not influence the shadow color. And instead we can customize shadow by changing android:outlineAmbientShadowColor and android:outlineSpotShadowColor attributes - they have reasonable default values (for default shadows to be similar to shadows on previous api versions) set in app theme but also their values can be overriden for each separate view:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:text="Button with default theme shadow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button2"
android:outlineAmbientShadowColor="#55FF0000"
android:outlineSpotShadowColor="#55FF0000"
android:text="Button with custom red shadow"
app:layout_constraintTop_toBottomOf="#id/button1"
app:layout_constraintStart_toEndOf="#id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</androidx.constraintlayout.widget.ConstraintLayout>

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>

EditText underline below text property

I would like to change the blue colour below the edit text, i don't know what property it is.
I tried using a different background colour for it but it didn't work.
I've attached an image below:
It's actually fairly easy to set the underline color of an EditText programmatically (just one line of code).
To set the color:
editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
To remove the color:
editText.getBackground().clearColorFilter();
Note: when the EditText has focus on, the color you set won't take effect, instead, it has a focus color.
API Reference:
Drawable#setColorFilter
Drawable#clearColorFilter
Use android:backgroundTint="" in your EditText xml layout.
For api<21 you can use AppCompatEditText from support library thenapp:backgroundTint=""
You have to use a different background image, not color, for each state of the EditText (focus, enabled, activated).
http://android-holo-colors.com/
In the site above, you can get images from a lot of components in the Holo theme. Just select "EditText" and the color you want. You can see a preview at the bottom of the page.
Download the .zip file, and copy paste the resources in your project (images and the XML).
if your XML is named: apptheme_edit_text_holo_light.xml (or something similar):
Go to your XML "styles.xml" and add the custom EditText style:
<style name="EditTextCustomHolo" parent="android:Widget.EditText">
<item name="android:background">#drawable/apptheme_edit_text_holo_light</item>
<item name="android:textColor">#ffffff</item>
</style>
Just do this in your EditText:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/EditTextCustomHolo"/>
And that's it, I hope it helps you.
This works fine for old and new version of Android (works fine even on API 10!).
Define this style in your styles.xml:
<style name="EditText.Login" parent="Widget.AppCompat.EditText">
<item name="android:textColor">#android:color/white</item>
<item name="android:textColorHint">#android:color/darker_gray</item>
<item name="colorAccent">#color/blue</item>
<item name="colorControlNormal">#color/blue</item>
<item name="colorControlActivated">#color/blue</item>
</style>
And now in your XML, set this as theme and style (style to set textColor, and theme to set all other things):
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
style="#style/EditText.Login"
android:theme="#style/EditText.Login"/>
Edit
This solution causes a tiny UI glitch on newer Android versions (Lollipop or Marshmallow onwards) that the selection handles are underlined.
This issue is discussed in this thread. (I haven't tried this solution personally)
you can change Underline of EditText color specifying it in styles.xml. In your app theme styles.xml add the following.
<item name="android:textColorSecondary">#color/primary_text_color</item>
As pointed out by ana in comment section
<item name="android:colorControlActivated">#color/black</item>
setting this in theme style works well for changing color of an edittext underline.
So, you need to create a new .xml file in your drawable folder.
In that file paste this code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="8dp"
android:left="-3dp"
android:right="-3dp"
android:top="-3dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/white"/>
</shape>
</item>
</layer-list>
And in your EditText, set
android:background="#drawable/your_drawable"
You can play with your drawable xml, set corners, paddings, etc.
In your app style define the property colorAccent. Here you find an example
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/action_bar</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/action_bar</item>
</style>
You can change the color of EditText programmatically just using this line of code easily:
edittext.setBackgroundTintList(ColorStateList.valueOf(yourcolor));
To change bottom line color, you can use this in your app theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorControlNormal">#c5c5c5</item>
<item name="colorControlActivated">#ffe100</item>
<item name="colorControlHighlight">#ffe100</item>
</style>
To change floating label color write following theme:
<style name="TextAppearence.App.TextInputLayout" parent="#android:style/TextAppearance">
<item name="android:textColor">#4ffd04[![enter image description here][1]][1]</item>
</style>
and use this theme in your layout:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:hintTextAppearance="#style/TextAppearence.App.TextInputLayout">
<EditText
android:id="#+id/edtTxtFirstName_CompleteProfileOneActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:capitalize="characters"
android:hint="User Name"
android:imeOptions="actionNext"
android:inputType="text"
android:singleLine="true"
android:textColor="#android:color/white" />
</android.support.design.widget.TextInputLayout>
Use below code to change background color of edit-text's border.
Create new XML file under drawable.
abc.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#00000000" />
<stroke android:width="1dip" android:color="#ffffff" />
</shape>
and add it as background of your edit-text
android:background="#drawable/abc"
If you don't have to support devices with API < 21, use backgroundHint in xml, for example:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="Task Name"
android:ems="10"
android:id="#+id/task_name"
android:layout_marginBottom="15dp"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textColorLink="#color/blue"
android:textColorHint="#color/blue"
android:backgroundTint="#color/lighter_blue" />
For better support and fallbacks use #Akariuz solution.
backgroundHint is the most painless solution, but not backward compatible, based on your requirements make a call.
change your colorAccent which color you need that color set on colorAccent and run you get the output
Simply change android:backgroundTint in xml code to your color
You can do it with AppCompatEditText and color selector:
<androidx.appcompat.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:backgroundTint="#color/selector_edittext_underline" />
selector_edittext_underline.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/focused_color"
android:state_focused="true" />
<item android:color="#color/hint_color" />
</selector>
NOTE: Put this selector file in res/color folder, not res/drawable. ususually res/color does not exist and we may have to create it.

Categories

Resources