How to disable swimming when showing errors in TextInputLayout

<android.support.design.widget.TextInputLayout
        android:id="@+id/productLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:errorEnabled="true">

  <EditText
      android:id="@+id/product"
      android:layout_width="match_parent"
      android:layout_height="@dimen/margin_padding_width_height_6"
      android:cursorVisible="false"
      android:drawableRight="@drawable/ic_arrow_down"
      android:focusableInTouchMode="false"
      android:hint="@string/product"
      android:inputType="none"
      android:paddingEnd="@dimen/margin_padding_width_height_2"
      android:paddingRight="@dimen/margin_padding_width_height_2"
      android:singleLine="true"
      android:textSize="@dimen/text_size_m" />

private boolean validateFields() {
        if (mCategory.getText().toString().isEmpty())
            mCategoryLayout.setError("Please select a category");
        else if (mProducts.getText().toString().isEmpty())
            mProductsLayout.setError("Please select a product");
        else if (mSerialNumber.getText().toString().isEmpty())
            mSerialNumberLayout.setError("Please enter the serial number");
        else
            return true;
        return false;
    }

I executed the click listener function for EditText. Therefore, I do not want the inscription to EditTextbe on top when setting the error to TextInputLayout. How to disable this?

+4
source share
4 answers

Starting from version 23.2.0 of the support library you can call

setHintEnabled(false)

or putting it in your XML TextInputLayout as such:

app:hintEnabled="false"

Although the name may make you think that it removes all the hints, it just removes the floating one.

Related documents and release: http://developer.android.com/reference/android/support/design/widget/TextInputLayout.html#setHintEnabled(boolean)

https://code.google.com/p/android/issues/detail?id=181590

+4

- , , setError() on TextInputLayout, , 25.4.0. , .

public class HackedTextInputLayout extends TextInputLayout {

    private int forceExpandedHintBlockFlags = 0;

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);

        if (child instanceof EditText) {
            ((EditText) child).addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (!TextUtils.isEmpty(getError()) && TextUtils.isEmpty(s)) {
                        forceExpandedHintBlockFlags++;
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (!TextUtils.isEmpty(getError()) && TextUtils.isEmpty(s)) {
                        forceExpandedHintBlockFlags--;
                    }
                }
            });
        }
    }

    @Override
    public void setError(@Nullable CharSequence error) {
        forceExpandedHintBlockFlags++;
        super.setError(error);
        forceExpandedHintBlockFlags--;
    }

    @Nullable
    @Override
    public CharSequence getError() {
        // the main hack is right here
        return forceExpandedHintBlockFlags > 0 ? null : super.getError();
    }

    @Override
    public void setHintTextAppearance(int resId) {
        forceExpandedHintBlockFlags++;
        super.setHintTextAppearance(resId);
        forceExpandedHintBlockFlags--;
    }

    @Override
    protected void drawableStateChanged() {
        forceExpandedHintBlockFlags++;
        super.drawableStateChanged();
        forceExpandedHintBlockFlags--;
    }
}

TextInputLayout. setError(), TextWatcher updateLabelState(), :

if (hasText || this.isEnabled() && (isFocused || isErrorShowing)) {
    if (force || this.mHintExpanded) {
        this.collapseHint(animate);
    }
} ...

isErrorShowing = !TextUtils.isEmpty(this.getError()). , getError(), isErrorShowing == false . Voila:)

+1

:

mCategoeyLayout.setHintAnimationEnabled(false);

(, Edittext)

mCategoeyLayout.setHintAnimationEnabled(true);
0
  private boolean validateFields() {
    if (mCategory.getText().toString().isEmpty())
        mCategoryLayout.setError("Please select a category");
    else if (mProducts.getText().toString().isEmpty())
        mProductsLayout.setError("Please select a product");
    else if (mSerialNumber.getText().toString().isEmpty())
        mSerialNumberLayout.setError("Please enter the serial number");
    else
        return true;
    return false;
}
-1

All Articles