Home » Android » Android setError() message scrolling on top of ActionBar

Android setError() message scrolling on top of ActionBar

Posted by: admin May 14, 2020 Leave a comment

Questions:

My issue is that the error message in my EditTexts are scrolling on top of the action bar. Is this an android bug, or am I doing something wrong?

Also this bug happens even when the keyboard is down, it just happens to be up in the screenshots.

Here is what I am talking about: http://imgur.com/fG8bd3E,uFzhOXa#1 <- there are two images

Here is my layout, I add the EditTexts in my code to the kiosk_form_table LinearLayout

<ScrollView
    style="@style/DefaultBackground"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.6"
    android:fadeScrollbars="false"
    android:padding="20dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingRight="20dp" >

        <LinearLayout
            android:id="@+id/kiosk_form_table"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

        <Button
            android:id="@+id/kiosk_submit_large"
            style="@style/LoginButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:onClick="submit"
            android:text="@string/submit" />
    </LinearLayout>
</ScrollView>

How I add the EditTexts

kiosk_form.addView(getKioskRow(R.layout.kiosk_text_row, "First Name", true, null));

private View getKioskRow(int layout, String header, boolean required, String buttonName)
{
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(layout, null);
    view.setTag(required);
    addHeader((TextView) inflater.inflate(R.layout.field_header, null), header);
    if (required)
        ((TextView) view.findViewById(R.id.kiosk_input)).setHint("Required");
    if (buttonName != null)
        ((Button) view.findViewById(R.id.kiosk_input_button)).setText(buttonName);
    return view;
}

private void addHeader(TextView view, String header)
{
    view.setTag(false);
    view.setText(header);
    kiosk_form.addView(view);
}

How I set the error

(TextView) view).setError(getString(R.string.error_field_required));
How to&Answers:

Where do you want to see the error message? If you want it on EditText, set it on editText.

((EditText) findViewByID(R.id.my_edit_text)).setError ...... // and so on

Edit: original answer didn’t help.

Here’s one possible solution: you can hide the error message when the user is scrolling the view. When the scroll has finished you can re-set the error message and it should appear in the right spot! Here’s how you can detect when a scroll finished:
Android: How to detect when a scroll has ended

Answer:

It is Android OS bug.
You can track its progress here

Answer:

I resolve similiar problem by setting flag

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

Answer:

I had the same problem. Solved it like this:

private void processingErrorPopupsWhenScrolling(Rect scrollBounds, TextInputEditText... views) {
    boolean isViewInFocus = false;
    for (TextInputEditText textInputEditText : views) {
        if (textInputEditText.getError() != null) {
            if (textInputEditText.getLocalVisibleRect(scrollBounds)) {
                textInputEditText.setVisibility(View.VISIBLE);

                if (!isViewInFocus) {
                    textInputEditText.requestFocus();
                    isViewInFocus = true;
                }
            } else {
                textInputEditText.setVisibility(View.INVISIBLE);
            }
        }
    }
}

// in onCreate(){
Rect scrollBounds = new Rect();
mScrollView.getHitRect(scrollBounds);

mScrollView.getViewTreeObserver().addOnScrollChangedListener(() ->
        processingErrorPopupsWhenScrolling(scrollBounds,
                mLink,
                mTitle,
                mPrice)
);}