9

Possible Duplicate:
Close/hide the Android Soft Keyboard

I'm a beginner, and have written a simple program to find the roots of a quadratic equation. Entering values in the EditText fields works well, because the virtual keypad pops up so that you can enter your numbers. However, the keypad covers the TextView where your results appear. If the user knows it, they can press the "back" key, and the keypad is removed, revealing the results field. But I want the keypad to disappear when the user touches the "execute" button in the app without pressing the "back" key.

I have been researching this, and some suggested using finish(); But this doesn't only remove the keypad, it also exits the whole program.

So what's the easiest way to only remove the keypad, leaving the underlying TextView displayed? I want to include this in the onClick view that executes the math.

Any suggestions are appreciated.

Community
  • 1
  • 1
David Brown
  • 107
  • 4

1 Answers1

4

Just add this code in the onClick method:

InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(yourSubmiBtn.getWindowToken(), 0);
gezdy
  • 3,114
  • 2
  • 12
  • 14
  • Thank you very much -- looks just like what I need. However, being a beginner, I've had a little trouble with implementation. Eclipse told me that I needed to create a Context local variable, which I did. Then it wanted me to initialize it, so I initialized it to "null". But the program crashed. (Null pointer exception, of course.) Is initializing the mContext to null correct? – David Brown Dec 27 '12 at 21:10
  • I had added the declaration "Context mContext = null;" as a local variable. Eclipse says, "Null pointer access: The variable mContext can only be null at this location." I'm not sure what this means. – David Brown Dec 27 '12 at 21:32
  • the context is your Activity instance. If onClick method is in your activity class replace mContext by MyActivity.this (where MyActivity is your current activity name) – gezdy Dec 27 '12 at 22:30
  • Gezdy, this worked perfectly. Thank you! – David Brown Dec 28 '12 at 02:28