android -Edit Text background get red on TextInputLayout error - android

It's very strange problem, this is my code:
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_pass"
android:layout_width="match_parent"
android:layout_height="37dp"
android:layout_marginTop="10dp"
android:layoutDirection="rtl"
android:src="#drawable/ic_https_grey600_18dp"
app:hintEnabled="false"
app:passwordToggleEnabled="true">
<EditText
android:id="#+id/password"
style="#style/edittexts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_https_grey600_18dp"
android:drawableStart="#drawable/ic_https_grey600_18dp"
android:hint="رمز عبور"
android:gravity="right|center_vertical"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:transformPivotX="10dp"
app:passwordToggleEnabled="true" />
</android.support.design.widget.TextInputLayout>
I am using TextInputLayout for my login process, this is my java code :
pass_l.setErrorEnabled(true);
pass_l.setError(getString(R.string.wrong_pass));
When it show the error, the background of edit text gets red and it doesn't show the error below it , some thing like this :
how can I fix this problem? I don't want the edittext change the background color , just show the error below the editext as usual

Problem is with your style="#style/edittexts".Please shhare source code of it.It must be set Red in this style.

<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_pass"
android:layout_width="match_parent"
android:layout_height="37dp"
android:layout_marginTop="10dp"
android:layoutDirection="rtl"
android:theme="#style/TextLabel"
android:src="#drawable/ic_https_grey600_18dp"
app:hintEnabled="false"
app:passwordToggleEnabled="true">
<EditText
android:id="#+id/password"
style="#style/edittexts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_https_grey600_18dp"
android:drawableStart="#drawable/ic_https_grey600_18dp"
android:hint="رمز عبور"
android:gravity="right|center_vertical"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:transformPivotX="10dp"
app:passwordToggleEnabled="true" />
</android.support.design.widget.TextInputLayout>
<style name="TextLabel" parent="TextAppearance.AppCompat">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">#color/Color Name</item>
<item name="android:textSize">20sp</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">#color/Color Name</item>
<item name="colorControlNormal">#color/Color Name</item>
<item name="colorControlActivated">#color/Color Name</item>
</style>
You can try like this.

you can use this in your textInputLayoutTag :
app:errorEnabled="true"
app:errorTextAppearance="#style/ErrorAppearance"
example :
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_pass"
android:layout_width="match_parent"
android:layout_height="37dp"
android:layout_marginTop="10dp"
android:layoutDirection="rtl"
android:src="#drawable/ic_https_grey600_18dp"
app:hintEnabled="false"
app:passwordToggleEnabled="true"
app:errorEnabled="true"
app:errorTextAppearance="#style/ErrorAppearance">
<android.support.v7.widget.AppCompatEditText
android:id="#+id/password"
style="#style/FormStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_https_grey600_18dp"
android:drawableStart="#drawable/ic_https_grey600_18dp"
android:hint="رمز عبور"
android:gravity="right|center_vertical"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:transformPivotX="10dp"
app:passwordToggleEnabled="true" />
</android.support.design.widget.TextInputLayout>
add ErrorAppearance and FormStyle in the styles.xml
<style name="ErrorAppearance" parent="#android:style/TextAppearance">
<item name="android:textColor">#color/textError</item>
<item name="android:textSize">#dimen/font_small</item>
</style>
<style name="FormStyle">
<item name="android:maxLines">1</item>
<item name="android:drawablePadding">#dimen/spacing_small</item>
<item name="android:textColor">#color/textHint</item>
</style>
after the validation in the activity , you can :
pass_l.setError(getString(R.string.wrong_pass));

Related

Theme for OutlinedBox.TextInputLayout not Working

Hey I am trying to create a dropdown box with the OutlinedBox theme but the theme is not working.
this is my xml code -
android:id="#+id/add_transaction_category_parent"
style="#style/Widget.Material3.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="300dp"
android:layout_height="80dp"
android:layout_marginTop="4dp"
android:theme="#style/TextInputLayoutStyle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/add_transaction_date_parent">
<AutoCompleteTextView
android:id="#+id/add_transaction_category"
android:layout_width="match_parent"
android:layout_height="60dp"
android:enabled="true"
android:hint="category"
android:inputType="none"
android:textColorHint="#color/white"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>
and this is the theme I have defined for the TextInputLayout
<style name="TextInputLayoutStyle" parent="Widget.Material3.TextInputLayout.OutlinedBox">
<item name="boxStrokeWidth">1dp</item>
</style>
this is the output UI
Output UI
thanks!
try this
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="10dp"
android:ems="10"
app:hintTextColor="#color/white"
android:textColorHint="#color/white"
android:theme="#style/TextInputLayoutAppearance"
app:boxStrokeColor="#fff">
<EditText
android:id="#+id/textLogin_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:textColor="#color/white"
android:textColorHint="#color/white"
/>
</com.google.android.material.textfield.TextInputLayout>
create style.xml
<style name="TextInputLayoutAppearance" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="colorControlNormal">#color/white</item>
<item name="colorControlActivated">#color/white</item>
<item name="colorControlHighlight">#color/white</item>
</style>
</resources>

Is there any way to use a background for the hint in the TextInputLayout?

I need the hint to have this form when the input is disabled:
That is what I have achieved so far:
I tried to add a background using HintTextAppearance in the style, but it was not applied.
<style name="HintAppearance" parent="TextAppearance.Design.Hint">
<item name="android:background">#drawable/bg_material_form</item>
<item name="android:padding">0dp</item>
</style>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:hintTextAppearance="#style/HintAppearance">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="value" />
</com.google.android.material.textfield.TextInputLayout>

Material TextInputEditText not able to center hint and to keep input value moved to bottom

On https://material.io/components/text-fields there is an interactive demo for Filled TextField.
Label hint is centered inside input field and when you click on it and enter some text, label moves up and text that you inputed is slightly below center(below where hint previously was)
Problem is that I am not able to reproduce this behaviour on Android as text that I input stays in the same position as a placeholder.
Not centered hint and icon, but they should be
This is correct, as the label is moved up and password is below center line
Code:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/et_password"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
android:layout_width="0dp"
android:layout_height="65dp"
android:layout_marginTop="16dp"
android:background="#drawable/edittext_bg"
android:hint="#string/password"
app:boxStrokeWidth="1dp"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/tiet_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:inputType="textPassword"
android:textSize="17sp" />
</com.google.android.material.textfield.TextInputLayout>
try this
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:hint="First Name"
android:layout_height="wrap_content"
app:hintTextAppearance="#style/formTextAppearance">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/firstNameEditText"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="2dp"
android:importantForAutofill="no"
android:background="#null"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
android:inputType="textCapSentences"
android:maxLength="150"
android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
#styles ...
<style name="formTextAppearance" parent="#android:style/TextAppearance">
<item name="android:textColor">#android:color/black</item>
<item name="android:textColorHint">#android:color/black</item>
<item name="colorAccent">#android:color/black</item>
<item name="colorControlNormal">#android:color/black</item>
<item name="colorControlActivated">#android:color/black</item>
<item name="android:textSize">14sp</item>
</style>

OutlinedBox for TextInputEditText is not working

I am working on login screen where I want to implememt material edit text with following view :
Following is my code :
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
style="#style/ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="This is Hint"/>
</com.google.android.material.textfield.TextInputLayout>
But OutlinedBox is not appearing. This is how it looks :
PLease help.
styles.xml theme as follows,
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
TextInputLayout sample:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="#string/user_name" />
</com.google.android.material.textfield.TextInputLayout>
Hope this will work for you!
Use this
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
Instead pf this
style="#style/ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox"
SAMPLE CODE
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="This is Hint"/>
</com.google.android.material.textfield.TextInputLayout>
OUTPUT
For Material 1.2.x library, please make sure your custom style should be the descendants of your app theme, otherwise will get error:
: IllegalArgumentException: The style on this component requires your app
theme to be Theme.MaterialComponents
This took me whole day to figure out:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.Bridge">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:statusBarColor" tools:targetApi="lollipop">#color/colorGreen</item>
<item name="bottomSheetDialogTheme">#style/AppBottomSheetDialogTheme</item>
<item name="textInputStyle">#style/TextInputLayoutStyle</item> // add it here
</style>
<style name="TextInputLayoutStyle"
parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxStrokeColor">#color/color_green</item>
<item name="boxStrokeWidth">1dp</item>
<item name="errorEnabled">true</item>
<item name="hintTextColor">#color/text_color</item>
</style>
Just changed the AppTheme to "Theme.MaterialComponents.Light.DarkActionBar" and it worked for me...
I had the same problem and I found out that my activity was declared like this:
public class MyActivity extends Activity {
}
And I had to change it to this:
public class MyActivity extends AppCompatActivity {
}
Also, as mentioned in other answers, check the AppTheme MaterialComponents
In my case it was difficult to move app from Theme.AppCompat.Light.NoActionBar to Theme.MaterialComponents.Light.NoActionBar.
But it is posible to set theme only to View (in my case TextInputLayout). At the end I have
<com.google.android.material.textfield.TextInputLayout
android:theme="#style/Theme.MaterialComponents.Light.NoActionBar"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:id="#+id/textField"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginStart="16dp"
android:layout_marginTop="23dp"
android:layout_marginEnd="16dp"
android:hint="Enter Pin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView24">
<com.google.android.material.textfield.TextInputEditText
style="#style/Widget.MaterialComponents.TextInputEditText.OutlinedBox"
android:id="#+id/secureEditTextAuthorizePin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword" />
</com.google.android.material.textfield.TextInputLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/default_text_input_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Default"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/default_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/filled_text_input_layout"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="FilledBox"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/default_text_input_layout">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/filled_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/outlined_text_input_layout"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="OutlinedBox"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/filled_text_input_layout">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/outlined_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Check Once this Answer------------

How to change the colour of the unfocused edittext hinttext colour in TextInputLayout?

I need to change the colour of the unfocused edittext hinttext colour in TextinputLayout. I tried like below. Everything works fine except hint text colour of unfocused edittext. What am i doing wrong here?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="#style/AppTheme">
<EditText
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:hint ="Email"
android:maxLines="1"
android:textColor="#000000" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="#style/AppTheme">
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/passwordText"
android:hint="Password"
android:maxLines="1"
android:textColor="#000000"
android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
And my style:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorAccent</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColorHint">#color/colorAccent</item>
<item name="android:textSize">18sp</item>
<item name="colorControlNormal">#6E6D6D</item>
</style>
Above image is my output, if Email edittext is highlighted means, Password field hinttextcolor should be grey color like password field grey line.
All you have to do is just removing some lines from your code. Remove unnecessary styles from your styles and remove app:hintTextAppearance="#style/AppTheme" from your TextinputLayout.
Your style should be like this.
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorAccent</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textSize">18sp</item>
</style>
Your XML should be like this.
<android.support.design.widget.TextInputLayout
android:textColorHint="#404040" // Change color as per your wish
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:hint ="Email"
android:maxLines="1"
android:textColor="#000000" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_marginTop="10dp"
android:textColorHint="#404040" // Change color as per your wish
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/passwordText"
android:hint="Password"
android:maxLines="1"
android:textColor="#000000"
android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>
I have tested it. It will work for you.
try this in your edit text
android:textColorHint="#color/gray"
You can apply reverse logic here.Like this:
Set all edittext's hint color to grey from layout file.
Add onFocusChanged listener to all edittext.
Whenever any edittext got focused, change the hint text color of that
particular edittext.
Hope it works.
Please try below code:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/mUserEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:imeOptions="actionNext"
android:inputType="text"
android:maxLines="1"
android:paddingLeft="10dp"
android:textColorHint="#color/hint_color"
android:textSize="13sp" />
</android.support.design.widget.TextInputLayout>
Remove android:textColorHint tag from the style file.
This is the easisest way. Just Copy+paste please
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Description">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:inputType="textMultiLine"/>
</android.support.design.widget.TextInputLayout>

Categories

Resources