user4302606
user4302606

Reputation:

setError for TextInputLayout showing Error

TextInputLayout working fine, when tried to set Error it showing error.

Code:

 if (etFirstName.getText().length() == 0) {
            etFirstName.requestFocus();
            tvFirstName.setError("Please enter firstname");
        } else {
            tvFirstName.setError(null);
            tvFirstName.setErrorEnabled(false);
        }

Check Log:

java.lang.RuntimeException: Failed to resolve attribute at index 24
            at android.content.res.TypedArray.getColor(TypedArray.java:401)
            at android.widget.TextView.<init>(TextView.java:692)
            at android.widget.TextView.<init>(TextView.java:629)
            at android.widget.TextView.<init>(TextView.java:625)
            at android.widget.TextView.<init>(TextView.java:621)
            at android.support.design.widget.TextInputLayout.setErrorEnabled(TextInputLayout.java:297)
            at android.support.design.widget.TextInputLayout.setError(TextInputLayout.java:344)

Upvotes: 10

Views: 25443

Answers (8)

Stephen
Stephen

Reputation: 10079

MainFragment.kt:

private fun onSubmit(){
   setErrorTextField(false)

}

 private fun setErrorTextField(error: Boolean){
         if(error){
            binding.textField.isErrorEnabled = true
            binding.textField.error = "Try again!"
         }else {
            binding.textField.isErrorEnabled = false
            binding.textInputEditText.text = null
         }
}

XML Layout:

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textField"
            style="@style/Widget.Unscramble.TextInputLayout.OutlinedBox"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/default_margin"
            android:hint="@string/enter_your_word"
            app:errorIconDrawable="@drawable/ic_error"
            app:helperTextTextAppearance="@style/TextAppearance.MaterialComponents.Subtitle1"
            app:layout_constraintBottom_toTopOf="@+id/submit"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView_instructions">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/text_input_edit_text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="textPersonName|textNoSuggestions"
                android:maxLines="1" />
        </com.google.android.material.textfield.TextInputLayout>

Upvotes: 0

Balaji Bal
Balaji Bal

Reputation: 80

Use this it never need Theme.AppCompat

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/tv_metadata_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/tv_metadata_Details"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>

Upvotes: -1

Kapil Choudhary
Kapil Choudhary

Reputation: 19

Sytle theme for textinputlayout :

<style name="HintInputTextAppearance" parent="TextAppearance.AppCompat">
<item name="android:textColor">@color/input_floating_color</item>
<item name="android:textSize">22dp</item>
<item name="colorAccent">@color/input_floating_color</item>
</style>
<style name="InputTextAppearance" parent="TextAppearance.AppCompat">
<item name="android:textColor">#ff000000</item>
<item name="android:textColorHint">#ff000000</item>
<item name="android:textSize">24dp</item>
<item name="colorAccent">#ff000000</item>
</style>

<style name="ErrorTextAppearance" parent="TextAppearance.AppCompat">
<item name="android:textColor">#ffff0000</item>
<item name="android:textColorHint">#ffff0000</item>
<item name="android:textSize">18sp</item>
<item name="colorAccent">#ffff0000</item>
</style>

Use in Layout as :

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:id="@+id/name_signup"
app:errorEnabled="true"
app:errorTextAppearance="@style/ErrorTextAppearance"
app:hintTextAppearance="@style/HintInputTextAppearance"
android:layout_marginLeft="@dimen/left_margin_login"
android:layout_marginRight="@dimen/right_margin_login"
android:layout_height="match_parent">
    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:textAppearance="@style/InputTextAppearance"
        android:layout_height="wrap_content"
        android:hint="Name" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:id="@+id/email_signup"
app:errorEnabled="true"
app:errorTextAppearance="@style/ErrorTextAppearance"
app:hintTextAppearance="@style/HintInputTextAppearance"
android:layout_marginLeft="@dimen/left_margin_login"
android:layout_marginRight="@dimen/right_margin_login"
android:layout_height="match_parent">
    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:textAppearance="@style/InputTextAppearance"
        android:layout_height="wrap_content"
        android:hint="Email" />
</android.support.design.widget.TextInputLayout>

Upvotes: 0

twelvester
twelvester

Reputation: 149

Solution by @Sevastyan works great for using xml layout.

However, if i have a TextInputLayout and a TextInputEditText which i created programmatically, and i did this:

tilCode.setErrorEnabled(true);
tilCode.setErrorTextAppearance(R.style.error);

I will get an error. But just:

tilCode.setErrorTextAppearance(R.style.error);

Works for me.

Upvotes: -1

Sevastyan Savanyuk
Sevastyan Savanyuk

Reputation: 6315

Although I did not fully understand why the problem occurs, it must have to do something about how Android applies colors to Views according to its states.

Here is the way I solved the problem:

1) Define custom style, that will be used only for styling the errors (or hints) only:

In your res/values/styles.xml add new style node:

    <style name="error" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/colorAccent</item> <!--apply the color you wat here -->
        <item name="android:textSize">12dp</item>
    </style>

2) Apply the style to your TextInputLayout:

Also, make sure, that you specify errorEnabled attribute:

<android.support.design.widget.TextInputLayout
        android:id="@+id/input_email_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        app:errorEnabled="true"
        app:errorTextAppearance="@style/error"
    >
        <android.support.design.widget.TextInputEditText
            android:id="@+id/input_email"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:inputType="textEmailAddress"
                  android:hint="@string/hint_email"/>
    </android.support.design.widget.TextInputLayout>

Upvotes: 15

Ravi Varma
Ravi Varma

Reputation: 66

Use TextUtils

if(TextUtils.isEmpty(etFirstName.getText().toString()))
{
   etFirstName.setError("Filed is mandatory!");
   view = etFirstName;
}

Upvotes: -3

Mehta
Mehta

Reputation: 1226

Try below code to show error in TextInputLayout:

 tvFirstName.setErrorEnabled(true);
 tvFirstName.setError("Please Enter firstName");

Upvotes: -5

Ranjithkumar
Ranjithkumar

Reputation: 18416

Give valid style for your TextInputLayout( android:theme="@style/Theme.AppCompat")

<android.support.design.widget.TextInputLayout
  android:id="@+id/testingInputLayout"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:theme="@style/Theme.AppCompat">

<EditText
   android:id="@+id/testingEditText"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:hint="@string/testText"
   android:inputType="textEmailAddress" />

</android.support.design.widget.TextInputLayout>

You need to add appCompat & support design dependency (if not)

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'

In your manifest file add AppCompat theme,

<application
 ...
android:theme="@style/Theme.AppCompat">

Upvotes: 7

Related Questions