Hide Soft Keyboard on Done Keypress in Android?

37,271

Solution 1

Use android:imeOptions="actionDone", like that:

<EditText
    ...
    android:imeOptions="actionDone" />

Solution 2

InputMethodManager inputManager = (InputMethodManager)
context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(0, 0);

with context being your activity.

Solution 3

Changed the if-statement to if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) made it working with the xml-attribute android:inputType="phone".

Solution 4

You should have a look at setOnEditorActionListener() for the EditText:

Set a special listener to be called when an action is performed on the text view. This will be called when the enter key is pressed, or when an action supplied to the IME is selected by the user.

Share:
37,271
Benny Skogberg
Author by

Benny Skogberg

Professional Solutions Architect BSc in Computer Science, major in Information Architecture MCSE: SharePoint MCSA: Office 365 | Linked In | Twitter |

Updated on October 17, 2021

Comments

  • Benny Skogberg
    Benny Skogberg over 2 years

    I'm struggling with the done button on the soft keyboard. I can't get the soft keyboard Done key press to hide the keyboard. From another button, it works perfectly with

    imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);

    but the onKeyListener does not function the way I want. When I hit the editText, the soft keyboard shows up and its content is cleared from characters.

    Thanks for listening!

    The main.xml:

    <EditText 
        android:id="@+id/answer" 
        android:layout_gravity="center_horizontal" android:textSize="36px"
        android:inputType="phone"
        android:minWidth="60dp" android:maxWidth="60dp"
    />
    

    The Java file:

    private EditText editText;
    //...
    editText = (EditText)findViewById(R.id.answer);
    editText.setOnClickListener(onKeyboard);
    editText.setOnKeyListener(onSoftKeyboardDonePress);
    //...
    
    // method not working:
    private View.OnKeyListener onSoftKeyboardDonePress=new View.OnKeyListener() 
    {
        public boolean onKey(View v, int keyCode, KeyEvent event) 
        {
            if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
            {
                // code to hide the soft keyboard
                imm = (InputMethodManager) getSystemService(
                    Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);
            }
            return false;
        }
    };
    
    private View.OnClickListener onKeyboard=new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            editText.setText("");
        }
    };
    

    The working method using a button (in the same java file):

    private View.OnClickListener onDone=new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            //....
            // code to hide the soft keyboard
            imm = (InputMethodManager) getSystemService(
                Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);
        }
    };
    

    Edit: When I press key no "9" the keyboard hides. That's odd.