6

Password field part of my xml code:

<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
...

    <android.support.design.widget.TextInputLayout
        android:layout_below="@+id/uname_ly"
        android:id="@+id/text_input_layout_passwd"
        app:layout_widthPercent="70%"
        android:layout_centerHorizontal="true"
        app:layout_heightPercent="10%"
        app:layout_marginTopPercent="0%"
        app:layout_marginBottomPercent="0%"
        android:adjustViewBounds="true"
        android:textColorHint="@color/editTextHintColor"
        app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout"
        >

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/nopasswd"
            android:inputType="textPassword"
            android:maxLines="1"
            android:textColor="@color/editTextTextColor" />

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

How to i remove this overlay red exclamation mark when call setError() ? enter image description here

[Update the style]

<style name="TextAppearance.App.TextInputLayout" parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/editTextHintColor</item>
</style>
sunadorer
  • 3,855
  • 34
  • 42
林果皞
  • 7,539
  • 3
  • 55
  • 70
  • 1
    See This one it will Help You http://stackoverflow.com/q/5218691/5773037 – Nikunj Paradva Oct 20 '16 at 11:56
  • 1
    this may help you http://stackoverflow.com/questions/14413575/how-to-write-style-to-error-text-of-edittext-in-android – H Raval Oct 20 '16 at 11:58
  • 2
    Call `setError()` on the `TextInputLayout`, instead of the `EditText`. – Mike M. Oct 20 '16 at 12:00
  • @MikeM. I tried `TextInputLayout mPasswordL = (TextInputLayout) rootView.findViewById(R.id.text_input_layout_passwd); `and `mPasswordLsetError(Html.fromHtml("" + getString(R.string.error_empty_password) + ""));` already but it doesn't work. – 林果皞 Oct 20 '16 at 12:03
  • @MikeM. Sorry, it did work but it remove the entire box and leave only red line+no text. – 林果皞 Oct 20 '16 at 12:05

3 Answers3

13

Use setError(CharSequence error, Drawable icon) method. Set the drawable as null:

editText.setError("Pls provide email", null);
Srijith
  • 1,695
  • 17
  • 29
6

TextInputLayout is now part of the new material package. Here is an update on how I got a password field with validation working:

Layout:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/loginPasswordInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/grid"
    android:hint="@string/onboarding_hint_password"
    app:errorEnabled="true"
    app:passwordToggleContentDescription="@string/onboarding_toggle_description_password"
    app:passwordToggleEnabled="true"
    app:passwordToggleTint="?colorAccent">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/loginPasswordInputText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start|center"
        android:inputType="textPassword"
        android:lines="1"
        tools:text="@string/onboarding_hint_password" />
</com.google.android.material.textfield.TextInputLayout>

Setting the error message and hiding the icon in code can then be achieved with the following:

loginPasswordInputLayout.error = errorMessage
// Error icon overlaps with password visibility toggle, so remove it:
loginPasswordInputLayout.setErrorIconDrawable(0)

This way you get the red (or colour of your choice) text and underline, but the password visibility toggle icon stays fully accessible.

sunadorer
  • 3,855
  • 34
  • 42
0

I think this is a more optimized answer if you are working with TextInputLayout. What I did was took advantage of the error and I just rest the Text input layout to its default state after 2 seconds.

otpIn.setError(obj.getString("status"));

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
otpIn.setError(null);
}
},2000);
koshur
  • 124
  • 1
  • 9