Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: why is my OnKeyListener() not called?

Tags:

android

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!

like image 705
mmo Avatar asked Sep 12 '10 23:09

mmo


3 Answers

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. onKeyListener not working with soft keyboard (Android)

like image 155
Cheryl Simon Avatar answered Oct 31 '22 11:10

Cheryl Simon


I had the exact same problem, but on only 1 of my Android apps and I never did figure out what the difference was.

My solution though was to do what Mayra suggested and add a TextWatcher to handle the TextChanged events. So it works no matter how the text entry occurs.

editName.addTextChangedListener(new TextWatcher () {
        public void afterTextChanged(Editable s) {
            Button btnSave = (Button)findViewById(R.id.btnSaveToon);
            if(s.length() > 0)
                btnSave.setEnabled(true);
            else
                btnSave.setEnabled(false);
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
        }

    });

Works like a charm in the emulator and on my HTC Inspire

like image 34
Kaptkaos Avatar answered Oct 31 '22 10:10

Kaptkaos


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.

like image 1
Tyler Avatar answered Oct 31 '22 12:10

Tyler