5

My Android application contains an EditText view where you can type some short messages (single line). Pressing the keyboard's DONE key will append the message to a log view above (TextView) and clear the input view.

Here's a snippet from my view xml:

<LinearLayout ...>
    <TextView
        android:id="@+id/logView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/inputView"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:imeOptions="actionDone"
        android:singleLine="true" />
</LinearLayout>

To handle the input and reset the view, I use the OnEditorActionListener.

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    ...
    String input = mInputView.getText().toString();
    mInputView.setText(""); // clear the input view
    ...
}

The problem

I did not experience any problems on Android 1.6 - 3. But starting with IceCreamSandwich (>= Android 4) there's a weird bug which occurs intermittently (in most cases after ~10-30 inputs).

When you type some text, the input view remains blank. The cursor still blinks on position 0, no text is shown. Though a click on DONE adds the (invisible) text to the log view above and the text can be read. Also hiding the keyboard makes the text in the EditText view visible.

Solution

As stated in the accepted answer this is a (not so much) known bug of the Android OS. The simple solution is to clear the EditText view in a different way:

TextKeyListener.clear(mInputView.getText());
ottel142
  • 2,016
  • 1
  • 26
  • 37

3 Answers3

3

I had exactly the same issue, even on lower API levels. There's a bug when using:

editText.setText("");

many times to empty an EditText. Here's a workaround that helped:

TextKeyListener.clear(editText.getText());

You can read about this bug on the Google Code site: http://code.google.com/p/android/issues/detail?id=17508

Hope it helps!

1

try setting an OnClickListener on your done button. Have the onClick(View v) look like this:

@Override
public void onClick(View v){
    kontextTV1.setText(editText1.getText.toString());
}

If you pull the text when the user hits the done button, you won't have to use a watcher class. This should also work on all versions of android. (Get/Set on edittext and textview aren't likely to change). That will handle a

If you want to handle the 'done' button on they keyboard, try:

editText1.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(final View v, final int keyCode, final KeyEvent event) {
    if (KeyEvent.KEYCODE_ENTER == keyCode) {
        //...
    }
}
edthethird
  • 6,263
  • 2
  • 24
  • 34
  • How can I add an OnClickListener on the DONE button of the virtual keyboard? I don't think that this is possible. If, though, does it also work on hardware keyboards and third-party virtual keyboard, e.g. HTC Sense? – ottel142 Feb 19 '12 at 13:00
  • I must have misread your question, edited response to show how to do this. – edthethird Feb 19 '12 at 16:31
  • Your solution works, but it does not fix the bug I've got using an OnEditorActionListener. After a series of inputs, the EditText does not show the input :( – ottel142 Feb 23 '12 at 23:24
0

Why not use afterTextChanged instead of editorActionListener?

Philip Sheard
  • 5,789
  • 5
  • 27
  • 42
  • The user submits his input with the DONE key, this is very important! Also an empty input has to be submittable. So I can't use a TextChangedListener, right? – ottel142 Feb 17 '12 at 22:24