Return to Snippet

Revision: 42438
at March 5, 2011 03:51 by BenClayton


Updated Code
textInput = (EditText)findViewById(R.id.textInput);
textInput.setInputType( InputType.TYPE_TEXT_VARIATION_URI ); // optional - sets the keyboard to URL mode
		
// kill keyboard when enter is pressed
textInput.setOnKeyListener(new OnKeyListener()
{
     /**
      * This listens for the user to press the enter button on 
      * the keyboard and then hides the virtual keyboard
      */
	public boolean onKey(View arg0, int arg1, KeyEvent event) {
        // If the event is a key-down event on the "enter" button
        if ( (event.getAction() == KeyEvent.ACTION_DOWN  ) &&
             (arg1           == KeyEvent.KEYCODE_ENTER)   )
        {               
        	InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(textInput.getWindowToken(), 0);   
                return true;
        }
        return false;
     }
} );

Revision: 42437
at March 5, 2011 03:50 by BenClayton


Initial Code
textInput = (EditText)findViewById(R.id.textInput);
textInput.setInputType( InputType.TYPE_TEXT_VARIATION_URI );
		
// kill keyboard when enter is pressed
textInput.setOnKeyListener(new OnKeyListener()
{
     /**
      * This listens for the user to press the enter button on 
      * the keyboard and then hides the virtual keyboard
      */
	public boolean onKey(View arg0, int arg1, KeyEvent event) {
        // If the event is a key-down event on the "enter" button
        if ( (event.getAction() == KeyEvent.ACTION_DOWN  ) &&
             (arg1           == KeyEvent.KEYCODE_ENTER)   )
        {               
        	InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(textInput.getWindowToken(), 0);   
                return true;
        }
        return false;
     }
} );

Initial URL


Initial Description
I kinda assumed the keyboard would automatically close when the ENTER key is pressed, but no it doesn't... Here's how..

Initial Title
Android: Close soft keyboard when ENTER button pressed

Initial Tags


Initial Language
Java