tags:

views:

167

answers:

2

I defined an EditText-field and I want to be informed when the user edits that fields. So I thought: simple - I add an OnKeyListener and so I did. But even though the text field gets edited (and even displays the entered/modified text) I don't get any callback, i.e. the LOG-output doesn't show up.

    TextView text = new TextView(this);
    text.setText(...);
    ...
    text.setOnKeyListener(new OnKeyListener()
    {                           
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            TextView tv = (TextView)v;
            CharSequence val = tv.getText();
            Log.v(TAG, "value: " + val);
            // ... rest omitted for brevity
        }
    });

Any idea, why that callback is never called?

Michael

PS.: Sigh! Android is really full of oddities! It seems that almost nothing I touched so far worked immediatly as one would expect. And - believe it or not - I have LOTS of experience with GUIs, esp. in Java (AWT, Swing, SWT, you name it...) But Android is a really tough beast!

+1  A: 

You say that you're dealing with an EditText, but your code refers to a TextView. My guess is that you have an EditText in your layout XML files, but you're referring to this newly created TextView in your code, which is in fact not even in the app's UI at all.

If there is already an EditText in your layout XML file, then you need to get a pointer to it in your Java code, probably using the findViewById() method. Then add your OnKeyListener to that EditText.

Defining your layout in XML actually makes a lot more sense (at least in many, if not most, cases) than defining it one component at a time and then adding each those components to the UI, like you do in Swing. But it takes some getting used to, no question.

MatrixFrog
@MatrixFrog: you are right - the TextView is in reality an EditText and not created by "new" but using an inflater. I changed that here for brevity. But I still don't get any onKey()-callbacks...
mmo
I could be wrong, but I don't think you want to use an inflater here. Just do `setContentView` and it does the inflating for you. My guess is that the same XML is getting inflated twice -- once via `setContentView` and once via `LayoutInflater`. So you have two copies of the same layout, and the listener is being added to the wrong one.
MatrixFrog
Nope, the issue is not that there are two copies. I even explicitly remove all children before starting to add my own ones. The issue seems to be the below effect - see Mayra's append.
mmo
+2  A: 

Are you using the soft keyboard (ime) to type in the edit text? I believe that the onKeyListener only gets invoked with events from the hardware keyboard. You are better off using the TextWatcher if you can. http://stackoverflow.com/questions/1967740/onkeylistener-not-working-with-soft-keyboard-android

Mayra
I read through the referenced append and that could indeed be the issue. Of course I never tried with a real keyboard (since my device doesn't have any...) and the emulator's keyboard apparently isn't triggering such events, either. I changed to a TextWatcher as suggested and that does the trick! Thanks!
mmo