0

I got a custom dialog. The numeric keyboard appears automatically when the dialog appears and edit field gets focus. But when I click on grey space around dialog as itself dialog disappear but keyboard is still open. I have tried Close/hide android soft keyboard but it's not working.

My code

private Button mSearchBtn;
private EditText mEditText;
private DialogUserSearchPresenter mPresenter;

public DialogUserSearch(@NonNull Context context) {
    super(context);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.dialog_search);
    mPresenter = new DialogUserSearchPresenter(new MainInteractor(new UserRepository(new Prefs(getContext())), new HotUsersRepository(new Prefs(getContext())), new SystemRepository(new Prefs(getContext()))));
    mEditText = (EditText) findViewById(R.id.dialog_search_edit);
    mEditText.requestFocus();
    InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    mSearchBtn = (Button) findViewById(R.id.dialog_search_button);
    mSearchBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mEditText.getText().length() == 10) {
                mPresenter.searchUser(mEditText.getText().toString().trim());
                closeKeyboard();
            } else {
                Toast.makeText(getContext(), getContext().getString(R.string.dialog_search_quantity_error),
                        Toast.LENGTH_LONG).show();
            }
        }
    });
}

@Override
protected void onStop() {
    super.onStop();
    closeKeyboard();
}



private void closeKeyboard() {
    View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) getContext().getSystemService(getContext().INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
Boris Ruzanov
  • 1,684
  • 1
  • 8
  • 21

1 Answers1

0

1)Can you try this:

private void closeKeyboard(View view) {
        if (view != null) {
            InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

call this way:

closeKeyboard(mEditText);

I tried it works

Kasım Özdemir
  • 4,028
  • 3
  • 12
  • 24