Zhen Liu
Zhen Liu

Reputation: 7932

Show EditText error when user is not focused on that editText

I have an editText that currently shows an error popover.

editText.setError("You have done something wrong");

Now I need a workflow such that, when user clicks on a button elsewhere, I need to set an error on the editText. But right now, only an error icon is shown.

My question is, is it possible to make it show the whole error text without the user actually clicking (focusing) on it?

Upvotes: 5

Views: 2911

Answers (2)

Jaythaking
Jaythaking

Reputation: 2102

I use that library MaterialEditText which makes everything more simple... Everything is well explained in the wiki page.

Helper Text and Error Text

helper text:

app:met_helperText="Integer"

error text:

just use original setError(CharSequence error) in java code.

regex check:

validationEditText.isValid("\\d+");

regex validate with error text setting:

validationEditText.validate("\\d+", "Only Integer Valid!");

Upvotes: 1

Manza
Manza

Reputation: 3527

Solution 1

Call

editText.requestFocus()

before setting the error.

Solution 2

Use a TextInputLayout.

    <android.support.design.widget.TextInputLayout
        android:id="@+id/text_input_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </android.support.design.widget.TextInputLayout>

And in code

textInputLayout.setError("Error");

Solution 3

Custom view, up to you.

Upvotes: 4

Related Questions