Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue: change border color or box stroke for unfocused TextInputLayout in android

I have a very specific issue for changing the outline of the textbox for TextInputLayout when it's unfocused. I can't seem to find an attribute to change the color for the border of my "unfocused" text box.

Here's a visual example of what I'm trying to do:

The color of this (textbox):border is not white. currently its not focused. After I click it, it turns white:

I don't know what I need to change, it doesn't seem like there is an attribute to change it.

I'm also using material design text input layout styles, although I don't see if that will affect it.

Here is my xml code for the text box:

 <other layouts ... >      <RelativeLayout             android:layout_width="match_parent"             android:layout_height="70dp"             android:layout_gravity="bottom"             android:layout_margin="5dp"             android:background="@drawable/item_recycler_view">              <android.support.design.widget.TextInputLayout                 android:id="@+id/dialog_text_input_layout"                 style="@style/Widget.AppTheme.TextInputLayoutList"                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:hint="Quick Add..."                 android:textColorHint="@color/colorWhite"                 app:boxStrokeColor="@color/colorWhite"                 app:errorEnabled="true"                 >                  <android.support.design.widget.TextInputEditText                     android:id="@+id/dialog_edit_text"                     android:layout_width="match_parent"                     android:layout_height="wrap_content"                     android:inputType="text"                     android:maxLines="1"                     android:textColor="@color/colorWhite"                     android:textSize="14sp" />             </android.support.design.widget.TextInputLayout>         </RelativeLayout>  </other layouts...> 

And here are the styles that I use for this:

<style name="TextAppearance.AppTheme.TextInputLayout.HintTextAlt" parent="TextAppearance.MaterialComponents.Subtitle2">     <item name="android:textColor">@color/colorWhite</item> </style>  <style name="Widget.AppTheme.TextInputLayoutList" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">     <item name="hintTextAppearance">@style/TextAppearance.AppTheme.TextInputLayout.HintTextAlt</item>     <item name="boxStrokeColor">@color/colorWhite</item>     <item name="boxCornerRadiusBottomEnd">5dp</item>     <item name="boxCornerRadiusBottomStart">5dp</item>     <item name="boxCornerRadiusTopEnd">5dp</item>     <item name="boxCornerRadiusTopStart">5dp</item>     <item name="android:layout_margin">5dp</item> </style> 

Thanks, any help or suggestions are welcome!

like image 248
Varun Govind Avatar asked Sep 05 '18 01:09

Varun Govind


People also ask

How do I remove TextInputLayout error?

Now you can simply do input. setError(..) for new error and input. setErrorEnabled(false) to remove it.

How do I disable TextInputLayout padding?

You can just set the start and end padding on the inner EditText to 0dp. Here's a screenshot with Show Layout Bounds turned on so you can see that the hints go all the way to the edge of the view.

What is TextInputLayout Android?

TextInputLayout is a view container that is used to add more features to an EditText. It acts as a wrapper for EditText and has some features like: Floating hint. Animation that can be disabled or enabled. Error labels that display error messages when an error occurs.


2 Answers

If you want to set the color for the outline box in unfocused mode instead of the default black, you have to add this line in colors.xml file which will override the default color for the outline box.

copy this line as it is. you can change color to what you want.

<color name="mtrl_textinput_default_box_stroke_color">#fff</color> 

until now it will work, for more control on TextInputLayout you may add this style in styles.xml

<style name="TextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">     <item name="boxStrokeColor">#fff</item>     <item name="boxStrokeWidth">2dp</item> </style> 

then add theme to TextInputLayout

android:theme="@style/TextInputLayoutStyle" 
like image 78
Amjad Avatar answered Oct 06 '22 05:10

Amjad


With the Material Components Library just use the boxStrokeColor attribute.
It can work with a selector.

Just use something like:

    <com.google.android.material.textfield.TextInputLayout         app:boxStrokeColor="@color/text_input_layout_stroke_color"         ..> 

with:

<selector xmlns:android="http://schemas.android.com/apk/res/android">   <item android:alpha="..." android:color="@color/...." android:state_focused="true"/>   <item android:alpha="..." android:color="@color/...." android:state_hovered="true"/>   <item android:alpha="..." android:color="@color/...." android:state_enabled="false"/>   <item android:alpha="..." android:color="@color/...."/>  <!-- unfocused --> </selector> 

enter image description hereenter image description here

like image 39
Gabriele Mariotti Avatar answered Oct 06 '22 04:10

Gabriele Mariotti