How to hide soft keyboard on android after clicking outside EditText? in fragment

i want to hide keyboard when user torches outside the Edit text but is not from activity but from fragment please any possible solution

Upvotes: 0

Views: 59

Answers (1)

Amirhosein
Amirhosein

Reputation: 4446

Try to get EditTesxt focus status with setOnFocusChangeListener and if it's not focused hide the keyboard:

    editText.setOnFocusChangeListener((v, hasFocus) -> {
        if (!hasFocus) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
        }
    });

Upvotes: 1

Related Questions