12

When I'm using edittext.setError("enter a comment") in android, it works fine until the keyboard suggestions come up and the error gets pushed above the edittext, after which it does not display the whole error message.

Why is it doing this?

After entering into the textbox, the balloon pop-up now appears above the textbox

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
Andy Lobel
  • 3,356
  • 9
  • 31
  • 40

5 Answers5

16

setError
Sets the right-hand compound drawable of the TextView to the "error" icon and sets an error message that will be displayed in a popup when the TextView has focus. The icon and error message will be reset to null when any key events cause changes to the TextView's text. If the error is null, the error message and icon will be cleared.

So when the text is changed it should be gone. I don't know why this doesn't happen in your case.

It should also be cleared when error message is null, so one trick could be:

edittext = (EditText)findViewById(R.id.foo); // add below this line
edittext.addTextChangedListener(new TextWatcher() {
    public void afterTextChanged(Editable s) {}
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){
        if(s != null && s.length() > 0 && edittext.getErrorMessage() != null) {
            edittext.setErrorMessage(null);
        }
    }
}); 
Caner
  • 57,267
  • 35
  • 174
  • 180
  • Post the code of the class where you call `edittext.setError("enter a comment")` – Caner Oct 12 '11 at 23:29
  • it wont let me for 8 hours and its too many characters for here but basically when the button in the picture 'update' is clicked it checks to see if the edittext is empty and if it is then show an error. Thanks xP – Andy Lobel Oct 12 '11 at 23:44
  • 1
    it works ! xP i have no idea how but it does haha your awesome thanks for the help x3 – Andy Lobel Oct 13 '11 at 00:13
  • 4
    There's a bug where certain character types don't reset the error. In my limited testing, I've found that alpha characters do not reset it while numbers and spaces do. This is a good workaround. Here's the reported issue: http://code.google.com/p/android/issues/detail?id=14310 – Pilot_51 Aug 30 '13 at 14:42
6

@Andy Lobel: I also faced this issue and have to fix it by putting on white-spaces(10-12) at the end of the text, so truncation happened only to white-spaces :) Also, my setError looked better by making setError text and EditText aligned.

Other Case: I was stuck on an another issue wherein, drawable icon is displayed but that floating message and its rectangular box didn't appear.

My Layout contained:

1) Username Edit Text

2) Password Edit Text

3) Confirm Password Edit Text

4) Register Button

So, I was validating and showing error at the time of click on Register Button but found out that the message failed to appear and only drawable used to come and found that message will appear only when the Edit Text is focusable as:

According to setError API Description:- Sets the right-hand compound drawable of the TextView to the "error" icon and sets an error message that will be displayed in a popup 'when the TextView has focus'.

So, message was for UserName Edit Text but last focus remained on Confirm Password Edit text, so, it never showed up

The solution/tweak for such case would be to:

EditText.setFocusableInTouchMode(true);
EditText.requestFocus();
EditText.setError("My Error Text");

Note: Wrote, just in case you are stuck on this point though other solutions might be available and sorry for so many editings as this is the best possible solution I came at last.

abhy
  • 933
  • 9
  • 13
  • 1
    I know this is quite old, but for the last part of your answer, just those 3 lines didn't worked for me. After I added `EditText.clearFocus();` before those 3 lines, everything worked like a charm ! – GAMA Nov 06 '13 at 14:54
  • Thanks, I appreciate your input. May be your layout gained focus on some other View rather EditText when it got loaded. Adding that will help to gain focus on that respective EditText View. – abhy Nov 11 '13 at 07:11
4

I have spent a lot of time trying various things to fix this ...

The easy fix: - make sure your error text is really really short

The fix that makes it all work:

When Android displays the softkeyboard, the view with your edit text gets "moved" up ... and the error text moves with this. The truncation usually happens as part of this. You can easily fix this by putting your entire layout in a ScrollView bracket ... in this way Android can move your EditText up by scrolling it with the entire layout - and then the error message will be fully displayed. Try it - it really works.

P.S: I like that you have posted a screenshot of your problem. Makes things a lot easier.

Stephan W
  • 41
  • 1
  • haha thanks, i havent touched java in ages but im sure it will be helpful to others, 1up you need some rep ;-) – Andy Lobel Jul 11 '12 at 21:58
  • @Stephan W This is what I did, for the parent layout of my fields is a `ScrollView`. Now the problem is that when the error messages are now showing and I scroll the view upwards, the error message goes up with the `EditText` even though that `EditText` is now within the view already. How do I fix this? – Compaq LE2202x Nov 21 '13 at 02:01
1

Another solution :
Adding android:windowSoftInputMode="adjustResize" on activity tag in AndroidManifest.xmlcorrected the issue for me

Irshad
  • 3,071
  • 5
  • 30
  • 51
Fisher
  • 11
  • 2
0

Set inputType="" value for EditText with the appropriate value android:inputType="textEmailAddress" and the popup will disappear when the first character is entered.

j0k
  • 22,600
  • 28
  • 79
  • 90