Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setOnKeyListener not responding

Tags:

android

I am new to Android and working through a to do list example from a book. I have one Activity which is displaying an EditText and a ListView beneath it. There is an onKey event which should add the text from the EditText to the ListView and clear the EditText. However, when I hit Enter on the keyboard all that happens is a new line is added to the EditText and nothing is added to the ListView.

The onCreate code is:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_to_do_list);

    // Get UI references
    ListView myListView = (ListView)findViewById(R.id.myListView);
    final EditText myEditText = (EditText)findViewById(R.id.myEditText);
    myEditText.setText("test");
    // Create ArrayList to store To Do items
    final ArrayList<String> toDoItems = new ArrayList<String>();

    // Create ArrayAdapter to bind items to List View
    final ArrayAdapter<String> aa;

    aa = new ArrayAdapter<String>(this,
                                  android.R.layout.simple_list_item_1,
                                  toDoItems);

    // Bind Adapter to List View
    myListView.setAdapter(aa);

    myEditText.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
                if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
                    (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    toDoItems.add(0, myEditText.getText().toString());
                    aa.notifyDataSetChanged();
                    myEditText.setText("");
                    return true;
                }
            return false;
        }
    });
}

I've added the myEditText.setText("test"); just to ensure the reference to myEditText is working, which it is. I've tried removing the if statements from the onKey event and it just doesn't appear to be registering key events at all. Can anyone advise what I'm doing wrong here?

like image 667
Niall Avatar asked Aug 10 '13 17:08

Niall


3 Answers

You could try using a OnEditorActionListener instead:

http://developer.android.com/reference/android/widget/TextView.html#setOnEditorActionListener(android.widget.TextView.OnEditorActionListener)

You could set on the edittext in XML:

android:imeOptions="actionDone"

and then in code:

 myEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
           // your action here
           return true;
        }
      return false;
    }
 )};
like image 189
natez0r Avatar answered Oct 24 '22 13:10

natez0r


Are you using the virtual keyboard? In that case the problem is that an OnKeyListener only gets called by hardware buttons. Take a look here: http://developer.android.com/reference/android/view/View.OnKeyListener.html

The first line says: Interface definition for a callback to be invoked when a hardware key event is dispatched to this view.

Perpaps something line this will solve your problem: Validating edittext in Android

like image 38
arnefm Avatar answered Oct 24 '22 14:10

arnefm


I was able to get that same example to work by adding the following attribute to the EditText element android:inputType="text". This changed the software keyboard that came up for the user and included a Send button.

In the setOnKeyListener code, I changed it to the following:

    myEditText.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_UP)
                if (keyCode == KeyEvent.KEYCODE_ENTER)
                {
                    todoItems.add(0, myEditText.getText().toString());
                    aa.notifyDataSetChanged();
                    myEditText.setText("");
                    return true;
                }
            return false;
        }
    });

Don't use KEYCODE_DPAD_CENTER as that's a hardware button for devices that had up, down, left, right arrows.

like image 24
krassinator Avatar answered Oct 24 '22 13:10

krassinator