material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

Attribute to customize Bounding box stroke

Open RowlandOti opened this issue 8 years ago • 26 comments

I can see that the bounding box for the TextInputLayout has been explicitly declared here:

 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);

i.e :

https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/textfield/TextInputLayout.java#L314

Are there plans to provide an attribute that allows developers, to for example override these? For example, use color states?

RowlandOti avatar May 18 '18 14:05 RowlandOti

I should mention that while this defaultStrokeColor looks alright on when using light backgrounds, it looks terrible with dark ones.

snotyak avatar Jun 10 '18 21:06 snotyak

Because the defaultBoxStrokeColor is currently private final int in TextInputLayout class you can change it only by overriding the color resource it uses. This is a temporary solution if you need to change this color until the issue gets resolved.

<color name="mtrl_textinput_default_box_stroke_color" tools:override="true">@color/white_40</color>

petrkubac avatar Jul 04 '18 07:07 petrkubac

Bad approach, @petrkubac we may want to use themes. Attributes are a better alternative.

RowlandOti avatar Jul 04 '18 10:07 RowlandOti

@RowlandOti I have updated my post to make it more understandable what I was talking about. You can still use themes, but if you need to change the default box stroke colour now (until the issue is resolved), you can override it like I did.

petrkubac avatar Jul 04 '18 12:07 petrkubac

Cool. Thanks for clarifying, it's currently the solution I was using, as it seems to be the only alternative at the moment. It is quite unsatisfactory, hence I look forward to a fix from the team.

RowlandOti avatar Jul 04 '18 14:07 RowlandOti

@afohrman any updates for this issue as i have more than one theme and in each one i want deferent default color for the box stroke

asmaa-adel avatar Jul 17 '18 12:07 asmaa-adel

What is status of this issue?

mrZizik avatar Jul 25 '18 18:07 mrZizik

We are aware of this issue and we plan to add a defaultBoxStrokeColor (or similarly named) attribute so that this value can be parameterized. We plan to do this after we finish the color system, but we'd be happy to accept a pull request for this if anyone would like to contribute.

afohrman avatar Jul 25 '18 20:07 afohrman

~petrkubac , what if we want to have two different TextInputLayouts depending on a theme. How can we use mtrl_textinput_default_box_stroke_color to set two different colours. TextInputLayout should be the very foundation of UI controls. Please provide a solution. Also it would be great if two other colours are attributes - mtrl_textinput_hovered_box_stroke_color and mtrl_textinput_disabled_color

selena775 avatar Sep 24 '18 09:09 selena775

Hi, @selena775 I don't think it is possible to do that without attributes that we can override in themes. The PR #181 could have helped, but I don't think it has been merged yet.

RowlandOti avatar Sep 24 '18 10:09 RowlandOti

@RowlandOti, in my opinion every color and every text style should be configurable. That way the component is reusable, easily customisable and it will make developers life much easier. It is not only the stroke color that is a problem here, it is also primaryColor used as text color and one cannot change it easily. I would be very grateful if you make TextInputLayout completely configurable. Thank you so much for reading this comment and maybe land my wish.

selena775 avatar Sep 24 '18 12:09 selena775

Hi all, I am writing to show my displeasure while figuring out the coloring of this element and hopefully someone will take a look at it.

Is there anything against using color state lists? Shouldn't it be possible to just give a statelist for the floating label, or the border, or the background, or whatever? Like all other Android components have? I have to override now mtrl_textinput_disabled_color, mtrl_textinput_default_box_stroke_color so I can follow a design for those colors when I disable the element, I have to access defaultStrokeColor through reflection, I have to call setBoxBackgroundColorResource manually when I enable/disable the view... This is very ugly in the code and also it was very time consuming to figure it all out. It is not my style to put hacky stuff into a production app, but there is no other way here (if I want to use official components)

joost-klitsie avatar Dec 18 '18 16:12 joost-klitsie

Hello everyone, as of the most recent version released (1.1.0-alpha02, although I believe it was available by alpha01) it is possible to use a color state list to change the stroke color of outlined text fields (through the boxStrokeColor attribute). (We currently use this color state list https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/textfield/res/color/mtrl_box_stroke_color.xml)

There are also different attributes for text/hint/helper texts colors and appearances (https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/textfield/res/values/styles.xml) and we are continuing the work to make text input layouts more configurable.

leticiarossi avatar Dec 18 '18 17:12 leticiarossi

@leticiarossi thanks for the info! I shall check the alpha version then.

joost-klitsie avatar Dec 18 '18 17:12 joost-klitsie

Thank you for the update -- I was able to successfully change the stroke colors for every state. 😄

Corsage avatar Dec 19 '18 03:12 Corsage

I have same issue. How can to do that? @Corsage @joost-klitsie

sinhpn92 avatar Feb 17 '19 04:02 sinhpn92

I believe I was able to do it with the following:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/background_light" android:state_focused="true"/>
    <item android:color="@android:color/background_light" android:state_hovered="true"/>
    <item android:color="@android:color/background_light" android:state_enabled="false"/>
    <item android:color="@android:color/background_light" />
</selector>

Take a note of the different android states, and then lastly the default state. Change the colors accordingly to what you need and then you can use it as a theme, an example is shown below.

<!-- Hint text appearance (used only when the background is colorPrimary). -->
    <style name="TextInputLayoutWhiteBox" parent="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
        <item name="boxStrokeColor">@color/layout_text_input_white</item>
        <!-- The color of the label when it is collapsed and the text field is active -->
        <item name="hintTextColor">@android:color/background_light</item>
        <!-- The color of the label in all other text field states (such as resting and disabled) -->
        <item name="android:textColorHint">@android:color/background_light</item>
    </style>

Corsage avatar Feb 19 '19 18:02 Corsage

I suffered a lot today and my suggestion would be to make defaultStrokeColor public like the other. This is not intuitive and dark theme looks really bad (of course stateList works, but it is not a 'perfect' solution).

screenshot_20190228-163503_sdk monitor

bernaferrari avatar Feb 28 '19 19:02 bernaferrari

Can you post your code that resulted in the outline being that color in your dark theme? It shouldn't look like that by default so I'm just trying to understand what customizations are being made.

dsn5ft avatar Feb 28 '19 22:02 dsn5ft

I think it was a bug that got fixed on one of the alphas, because I just tested on alpha04 and it is working as intended (just not in 1.0.0).

bernaferrari avatar Feb 28 '19 23:02 bernaferrari