6

I have several EditText objects in an app I'm working on, and need to learn how to dismiss the keyboard when the user is done entering text, so that buttons being blocked by the keyboard on the screen are visible again, and ready for action.

In Xcode, I've used the ResignFirstResponder method to do this when, for example, the "Done" button is clicked on the keyboard by the user. I'm assuming this is possible in Android as well, but I'm not sure. I appreciate any help!

embersofadyingfire
  • 523
  • 1
  • 4
  • 16

1 Answers1

9

Code to hide the Virtual Keyboard :

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

Put it inside the onClick() of your "Done" button and you will have reasons to believe that Android is as powerful as Xcode (if not more).

Swayam
  • 16,294
  • 14
  • 64
  • 102
  • Thanks for the info swayam! I have another question for you though. Is there a done button on an Android keyboard that can be implemented, just like in Xcode? – embersofadyingfire Jan 27 '13 at 19:51
  • Absolutely! In your EditText, set attribute `android:imeOptions="actionDone"` and you are good to go! :) – Swayam Jan 28 '13 at 15:26
  • Got another question for you swayam. I have implemented your suggestion, along with info from someone else. But now that I have the done button, and can clear out the keyboard, I'm realizing that the text being typed into the EditText is not being carriage returned when it reaches the maximum length of the EditText object. Do you have any suggestions on how to fix this? In Xcode i've been able to set an "auto-return" attribute, can this be done in Android? Thanks! – embersofadyingfire Feb 08 '13 at 01:44
  • I am sorry but I really dont understand what you mean by "carriage returned when the maximum length of the EditText is reached".. – Swayam Feb 08 '13 at 11:18
  • Would you mind elaborating on it please ? I really dont get what you are trying to achieve. Maybe post it as a separate question and give me the link here ? :) – Swayam Feb 08 '13 at 11:54
  • 1
    No worries anymore swayam, I've figured it out. Thanks for your help! – embersofadyingfire Feb 09 '13 at 06:18
  • Oh great! Great job! Good luck ahead!! :) – Swayam Feb 09 '13 at 08:48