I have a meme creator application, I have two text fields and a button, I want when the button is pressed to hide the keyboard, is this possible?
Asked
Active
Viewed 1.2k times
0
-
What have you tried this far? It's commonly expected on StackOverflow that questions show some effort in terms of actual code. – Mar 11 '16 at 13:08
-
first help yourself by trying it on your own – Vivek Mishra Mar 11 '16 at 13:08
-
Post your code what u have tried. – Jay Rathod Mar 11 '16 at 13:11
-
add to onClickListener [hide softkeyboard](http://stackoverflow.com/a/1109108/4149649) – Yuri Misyac Mar 11 '16 at 13:12
-
What have you tried on your own? Did you even try to search for this on google? If I google your title, you will get the answer in the first hit – 0xDEADC0DE Mar 11 '16 at 13:12
-
Sorry for my question, I've resolved it, I am noob at stackoverflow, I haven't thought that someone has the same question as me :)) – F. Dolha Mar 11 '16 at 13:17
5 Answers
7
public void dismissKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (null != activity.getCurrentFocus())
imm.hideSoftInputFromWindow(activity.getCurrentFocus()
.getApplicationWindowToken(), 0);
}
Activity have to be passed to this method, Keyboard will get dismissed.

Jeevanandhan
- 1,073
- 10
- 18
3
You can hide sof keyboard with this lines
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
Put this in the onClick(View view) event.
You need to import android.view.inputmethod.InputMethodManager
;
The keyboard will hides when you click the button.

Kristiyan Varbanov
- 2,439
- 2
- 17
- 37
0
What you want should already be happening. When you click the button, the focus changes from the text field to the button, so the keyboard will automatically hide.

jscs
- 63,694
- 13
- 151
- 195

Hariprasath
- 1
- 1
0
EditText editText = (EditText)findViewById(R.id.textBox);
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
this should be in button click event

Prabhashani
- 99
- 3
0
Android Kotlin
On button click hide the keyboard in kotlin
fun dismissKeyboard(activity: Activity) {
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
if (null != activity.currentFocus) imm.hideSoftInputFromWindow(
activity.currentFocus!!.applicationWindowToken, 0
)
}
And use in your class like this
button.setOnClickListener {
dismissKeyboard(this)
}

Aftab Alam
- 1,969
- 17
- 17