0

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?

Ali Khaki
  • 1,082
  • 1
  • 10
  • 22
F. Dolha
  • 93
  • 1
  • 2
  • 13

5 Answers5

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
  • 978
  • 8
  • 16
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,293
  • 1
  • 14
  • 27
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
  • 62,161
  • 12
  • 145
  • 186
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

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,193
  • 9
  • 12