Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Error popup on EditText doesn't move down when keyboard goes away

I have an activity that displays a few EditTexts on screen for user input. To be sure the soft keyboard doesn't cover my fields when it displays I have set the property

android:windowSoftInputMode="adjustPan"

for my Activity in the manifest. I am validating the EditText's content when 1. The view loses focus 2. When the user performs the 'Enter' action. Upon validation, if the value is not valid I am calling

setError(CharSequence error)

on the EditText, which causes a popup to display containing the error I passed in. The problem is if the EditText is moved up when the soft keyboard displays, and the popup is displayed at that time (validation has failed), the popup doesn't follow the EditText down when the keyboard goes away, it stays where it was first displayed.

Any ideas on how to fix this? Is this a bug in Android?

like image 290
Christopher Perry Avatar asked Jun 16 '11 20:06

Christopher Perry


3 Answers

You can also create Your custom EditText and override method onKeyPreIme(int keyCode, KeyEvent event)

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
        clearFocus();
        return false;
    }
}
like image 150
qbait Avatar answered Nov 13 '22 08:11

qbait


For me, it helped to wrap the layout in a ScrollView. After this, all the scrolling of the setError-box worked fine.

like image 42
Jockel Avatar answered Nov 13 '22 07:11

Jockel


If this is as you described, I think this may be a genuine bug, so may be worth writing it up on the Android Source site.

So evidently I can only think of hack work arounds!

Override when the keyboard disappears:

public boolean onKeyPreIme(int keyCode, KeyEvent event) {
 if (keyCode == KeyEvent.KEYCODE_BACK && 
     event.getAction() == KeyEvent.ACTION_UP) {
         revalidateEditText();
         return false;
 }
 return super.dispatchKeyEvent(event);
}

public void revalidateEditText(){
       // Dismiss your origial error dialog           
       setError(null);
       // figure out which EditText it is, you already have this code
       // call your validator like in the Q
       validate(editText); // or whatever your equivalent is
}

This will revalidate your EditText, dismiss your error dialog and re-show it.

How's that sound?

Inspired by: Get back key event on EditText

like image 11
Blundell Avatar answered Nov 13 '22 06:11

Blundell