13

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.

asked Dec 27, 2012 at 20:27
0

1 Answer 1

5

Just add this code in the onClick method:

InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
 imm.hideSoftInputFromWindow(yourSubmiBtn.getWindowToken(), 0);
answered Dec 27, 2012 at 20:39
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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.
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)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.