3

I am working on a messaging app in which I have an EditText for the user to type his message. I have provided a 'Send' button in keyboard with setImeOptions() method. However, whenever the user hits the 'Send' Button, the EditText loses focus. (I am doubtful about the word 'focus', but what I mean is the keyboard disappears..)

I find it a bit inconvenient as the user has to click on EditText again to get the keyboard after each send. I have tried editText1.requestFocus() in the code as follows :

editText1.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "send" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    // Perform action on key press
                    adapter.add(new OneComment(false, editText1.getText().toString()));
                    editText1.setText("");
                    editText1.requestFocus();
                    return true;
                }
                return false;
            }
        });

But this doesn't work...please suggest a workaround..thanks :)

tigerden
  • 728
  • 2
  • 11
  • 28

1 Answers1

2

You can try doing this

editText1.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId == EditorInfo.IME_ACTION_SEND){
            editText1.setText("");
            editText1.requestFocus();
            InputMethodManager imm = (InputMethodManager)getSystemService(Service.INPUT_METHOD_SERVICE);
            imm.showSoftInput(editText1, 0);
        }
        return true;
    }
});
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • The keyboard is still disappearing :( – tigerden Jun 30 '13 at 15:18
  • Thanks a lot for quick help... :) but Eclipse is showing error with getSystemService method...even after importing Service and InputMethodManager..any idea? – tigerden Jun 30 '13 at 15:27
  • oops made an mistake .. should call from context.. as this is not the context here .. check update .. the code is to show soft keyboard – stinepike Jun 30 '13 at 15:30
  • still not working.. [link](http://stackoverflow.com/a/17311598/2224551) will work? – tigerden Jun 30 '13 at 15:37
  • http://stackoverflow.com/questions/7200281/programatically-hide-show-android-soft-keyboard.. – stinepike Jun 30 '13 at 15:40
  • I read the post, but it is related to showing/hiding the keyboard at the start/resume of activity, I just need it to remain focussed after 'Send' is pressed.. – tigerden Jun 30 '13 at 15:52
  • 1
    yah .. but there it is also written how to show keyboard programmetically.. another last thing.. return true from the method.. check the last update ... If this not work i will give up :( – stinepike Jun 30 '13 at 15:55
  • yah that was my mistake.. uit always consume the default behavior return true :) .. – stinepike Jun 30 '13 at 16:00