TheOddCoder
TheOddCoder

Reputation: 161

Error-popup in EditText is not showing?

I have an EditTextField with a DatepickerListener on it. If I press a submit button it should validate that field and if they are empty it should show an error message. To display this error message I use the EditText.setError() method but the popup doesnt show. I dont know how to fix it.

I tested it on 2 Devices. Once with a OnePlus 5 on Android 7.1.1 and once on a Samsung Galaxy 7 on Android 7.0.0. Same behaviour on both devices.

Thank you in Advance.

Here is my Code (Dummy Version):

private void initRequestButton(View view) {
    Button button = view.findViewById(R.id.submitButton);
    EditText dateTextField = view.findViewById(R.id.someField);        

    button.setOnClickListener(v -> {
        String dateText = dateTextField.getText().toString();
        if (dateText.isEmpty()) {
                dateTextField.setError("A date is required");
        } else {
            //do Something
        }
    });
}

The Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
app:errorEnabled="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dip"
    android:layout_marginTop="10dip"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/someField"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dip"
        android:layout_marginTop="20dip"
        android:layout_weight="0.5"
        android:clickable="false"
        android:cursorVisible="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:inputType="none" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dip"
    android:layout_marginTop="5dip"
    android:orientation="horizontal">

    <Button
        android:id="@+id/submitButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0.3"
        android:text="@string/someText" />
</LinearLayout>

Upvotes: 3

Views: 1201

Answers (2)

Pratik Mhatre
Pratik Mhatre

Reputation: 779

The code you are using to set error is alright, but the popup pops only when the edittext is focussed either by touching manually or by programmatically, just add,

yourEdtext.requestFocus()

after setting error to the field. This will show the popup after validation fails

Upvotes: 0

user4571931
user4571931

Reputation:

Just remove the

 android:focusable="false"
    android:focusableInTouchMode="false"

properties from the edittext.

Upvotes: 3

Related Questions