customize Bounding box stroke in TextInputLayout.OutlinedBox - android

I'm trying to change box Stroke Color of com.google.android.material.textfield.TextInputLayout while not focused but there is no attribute for that this is my TextInputLayout code
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginTop="38dp"
android:layout_marginEnd="36dp"
android:textColorHint="#color/white"
app:boxStrokeColor="#color/white"
app:boxStrokeWidth="1dp">
<EditText
android:id="#+id/editTxt_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_email"
android:inputType="textEmailAddress"
android:textColorHint="#color/white" />
</com.google.android.material.textfield.TextInputLayout>
and I searched for a way to change its color and found this link :
https://github.com/material-components/material-components-android/issues/112
so i tried using this line in my colors file
<color name="mtrl_textinput_default_box_stroke_color" tools:override="true">#fff</color>
this solved the problem and changed stroke box color but the issue here is that i want to change this color in other TextInputLayouts in the same app !!

To change the box stroke color just use the app:boxStrokeColor attribute in the xml.
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
app:boxStrokeColor="#color/mySelector"
../>
You should use a selector. It is the default:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimary" android:state_focused="true"/>
<item android:alpha="0.87" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
<item android:alpha="0.12" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
<item android:alpha="0.38" android:color="?attr/colorOnSurface"/>
</selector>
You can also use a custom style:
<style name="MyOutlinedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxStrokeColor">#color/text_input_selector</item>
</style>

Related

selected color of MaterialToggleButton to a solid color

Not able to make the selected color of MaterialToggleButton to a solid color, only a transparent shade of color primary is shown.
I tried the below set of code and the out image is shown below.
Button theme in styles
<style name="ThemeButtonColorToggle" parent="AppTheme">
<item name="colorPrimary">#color/colorOrange</item>
<item name="colorButtonNormal">#color/colorOrange</item>
<item name="android:backgroundTint">#color/black</item>
<item name="strokeColor">#color/colorOrange</item>
<item name="strokeWidth">#dimen/mtrl_btn_stroke_size</item>
<item name="colorOnPrimary">#color/colorOrange</item>
<item name="colorOnPrimarySurface">#color/colorOrange</item>
</style>
XML code
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="#+id/toggleGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:checkedButton="#id/btnAndroid"
app:layout_constraintTop_toBottomOf="#id/tvName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:selectionRequired="true"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="#+id/btnAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:theme="#style/ThemeButtonColorToggle"
android:textColor="#android:color/white"
android:textColorHighlight="#color/colorOrange"
android:text="Android" />
<com.google.android.material.button.MaterialButton
android:id="#+id/btniOS"
android:layout_width="wrap_content"
android:textIsSelectable="true"
android:textColor="#android:color/white"
android:theme="#style/ThemeButtonColorToggle"
android:layout_height="wrap_content"
android:text="iOS" />
</com.google.android.material.button.MaterialButtonToggleGroup>
I require to get a solid color as shown below
can anyone please help me to sort out
The background color of the MaterialButton is defined by the backgroundTint attribute.
The default value is:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimary" android:state_enabled="true"/>
<item android:alpha="0.12" android:color="?attr/colorOnSurface"/>
</selector>
You can change it in the xml layout or you can define a custom style:
<com.google.android.material.button.MaterialButton
style="#style/ButtonStyle"
.. />
with:
<style name="ButtonStyle" parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">#color/your_button_background_selector</item>
...
</style>

Android: Make TextInputLayout Read-Only

How can I set a TextInputLayout to read only?
My code is as shown below:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/TextInputLayout_fromAddAbsenceBottomSheet_AbsenceTaken"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:hint="Absence Taken"
app:helperText="* Optional"
app:layout_constraintEnd_toEndOf="#+id/TextInputLayout_fromAddAbsenceBottomSheet_AbsenceGiven"
app:layout_constraintStart_toStartOf="#+id/TextInputLayout_fromAddAbsenceBottomSheet_AbsenceGiven"
app:layout_constraintTop_toBottomOf="#+id/TextInputLayout_fromAddAbsenceBottomSheet_AbsenceGiven"
style="#style/Widget.Unify.TextInputLayout">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/TextField_fromAddAbsenceBottomSheet_AbsenceTaken"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
style="#style/Widget.Unify.EditText"/>
</com.google.android.material.textfield.TextInputLayout>
I need to make the TextInputLayout read only NOT THE EditText.
I need to make it read only at the TextInputLayout level because I need to apply a style to it.
I've already tried android:enabled="false" in the TextInputLayout but it still lets me change the text.
android:focusable="false"
android:focusableInTouchMode="false"
make the TextInputEditText focusable false by default in your xml file
The question is not clear.
To disable the TextInputLayout just use:
<com.google.android.material.textfield.TextInputLayout
android:enabled="false"
..>
It works with the version 1.1.0 (currently 1.1.0-beta02) and 1.2.0 (currently 1.2.0-alpha02).
Result: enabled/disabled:
You can customize the box color using the app:boxStrokeColor and a selector.
Something like:
<com.google.android.material.textfield.TextInputLayout
android:enabled="false"
app:boxStrokeColor="#color/text_input_layout_stroke_color"
..>
where the selector is:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="..." android:state_focused="true"/>
<item android:alpha="0.87" android:color="..." android:state_hovered="true"/>
<item android:alpha="0.12" android:color="..." android:state_enabled="false"/>
<item android:color="..."/>
</selector>
where the color used in the disabled state is defined by <item android:alpha="0.12" android:color="..." android:state_enabled="false"/>.
Result: disabled with different boxStrokeColor.
If you want to customize the color of EditText you can use the android:textColor attribute:
<com.google.android.material.textfield.TextInputEditText
android:textColor="#color/..."
Result: disabled with different editext color.
Also in this case you can use a selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#color/..."/>
<item android:color="#color/...."/>
</selector>

TextInputLayout styling

I have problem with my custom TextInputLayout. Here is my code
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/phone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:theme="#style/CustomTextInputLayout"
android:hint="#string/phone_number"
android:layout_marginTop="8dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:startIconDrawable="#drawable/account"
android:background="#color/bg_light_gray"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/page_description">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="?attr/listPreferredItemHeight"
android:background="#color/bg_light_gray"
android:inputType="phone"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
Here is my code from style.xml file
<style name="CustomTextInputLayout" parent="Widget.Design.TextInputLayout">
<item name="errorTextAppearance">#style/ErrorText</item>
<item name="colorControlNormal">#color/green_txt</item>
<item name="colorControlActivated">#color/orange</item>
<item name="colorControlHighlight">#color/orange</item>
</style>
I want to make a baseline color of TextInputLayout to orange, but now it is gray. Also, I place an icon, which has its original color orange, but after placing it became dark gray. Now I am not understanding why it is happening and how to fix this problem. Can someone help me with that?
I want to make base line color of TextInputLayout to orange
Just use a Material theme (for example Widget.MaterialComponents.TextInputLayout.FilledBox) the app:boxStrokeColor attribute to change the color.
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/custom_end_icon"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox"
app:boxStrokeColor="#color/text_input_selector
where the selector is something like:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimary" android:state_focused="true"/>
<item android:alpha="0.87" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
<item android:alpha="0.12" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
<item android:alpha="0.38" android:color="?attr/colorAccent"/>
</selector>
Also I place an icon, which has its original color orange, but after placing it became dark gray.
Use the app:endIconTint attribute to apply a color or a color selector to your end icon.
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/custom_end_icon"
android:hint="Hint text"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox"
app:boxStrokeColor="#color/text_input_selector"
app:endIconMode="custom"
app:endIconDrawable="#drawable/ic_add_24px"
app:endIconTint="#color/text_input_selector"

Change a color of EditText's controls in the selected state

In my app I get a color scheme for the app from the server. I update colors for all app views according to the server settings. My question is related to updating colors of EditText in the selected state:
I can change a background color of selection, using the editText.setHighlightColor() method, but can I somehow change a color of blue drag views at the left and right bottom corners? I can't change the color, using the colorAccent style attribute, as I get the color from the server at runtime. Can I change the drag views dynamically?
Here is full example, custom TextInputLayout colors:
in your custom *.xml file, check properties style and android:theme
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/txtConfirmPassword"
style="#style/TextInputLayoutStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:hint="#string/confirm_password_hint"
android:importantForAutofill="no"
app:hintAnimationEnabled="false"
app:hintEnabled="false"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
style="#style/TextInputEditTextStyle"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="#string/password_hint"
android:inputType="textPassword"
android:singleLine="true"
android:theme="#style/TextInputEditTextTheme" />
</com.google.android.material.textfield.TextInputLayout>
in your themes.xml file
<style name="TextInputEditTextStyle">
<item name="android:textColor">#color/main_text</item>
</style>
<style name="TextInputEditTextTheme">
<item name="android:textColorHighlight">#color/gray2</item> <!-- background of selected text -->
<item name="android:colorControlActivated">#color/main_text</item> <!-- cursor -->
</style>
you can custom TextInputLayout's border, by adding this to themes.xml
<style name="TextInputLayoutStyle" parent="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="boxStrokeColor">#color/text_input_layout_tint</item>
<item name="boxStrokeWidth">1dp</item>
<item name="boxStrokeWidthFocused">1dp</item>
<item name="boxCornerRadiusBottomEnd">8dp</item>
<item name="boxCornerRadiusBottomStart">8dp</item>
<item name="boxCornerRadiusTopStart">8dp</item>
<item name="boxCornerRadiusTopEnd">8dp</item>
</style>
file #color/text_input_layout_tint:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/action_yes" android:state_focused="true" />
<item android:color="#color/stroke" />
</selector>

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!
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"
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>
Answer from Amjad was right, but starting from 1.1.0-alpha02 (probably even alpha01) after this issue was resolved it's possible to define color in color state list using the same boxStrokeColor attribute, e.g. how it's done in lib:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimary" android:state_focused="true"/>
<item android:alpha="0.87" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
<item android:alpha="0.12" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
<item android:alpha="0.38" android:color="?attr/colorOnSurface"/>
</selector>
Usage:
...
app:boxStrokeColor="#color/text_input_layout_stroke_color"
...
app:boxStrokeColor="#000000"
app:boxStrokeWidthFocused="0.5dp"
app:boxStrokeWidth="0.5dp"
This will make the box color constant event it is focused!

Categories

Resources