Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable floating while showing error 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 have implemented click listener for the EditText,So I don't want float the EditText label to top while setting error on TextInputLayout. How do I disable that ?

like image 344
Vaisakh N Avatar asked Mar 18 '16 11:03

Vaisakh N


People also ask

How do I turn off floating hint in TextInputLayout?

Enabling/Disabling Floating Hints Floating Hints are enabled by default in a TextInputLayout. To disable it we need to add the following attribute inside the tag : app:hintEnabled="false" . The below xml code is from the activity_main. xml layout and has three EditText fields.

How do I remove TextInputLayout error?

You should be able to call TextInputLayout. setErrorEnabled(false) to hide the error TextView and calling TextInputLayout. setError(error) now internally calls TextInputLayout. setErrorEnabled(true) if the error isn't null or empty.

How do I remove TextInputLayout padding?

You can remove extra space above AppCompatEditText by setting app:hintEnabled="false" to TextInputLayout but it won't display hint until you re-enable that. Save this answer. Show activity on this post. Save this answer.


1 Answers

Starting version 23.2.0 of the Support Library you can call

setHintEnabled(false)

or putting it in your TextInputLayout xml as such :

app:hintEnabled="false"

Though the name might makes you think it removes all hints, it just remove the floating one.

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

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

like image 148
Gauthier Avatar answered Oct 28 '22 15:10

Gauthier