Search code examples
androiddialogkeyboardfragment

How to hide keyboard after edittext click


I have a edittext in my android app where I can put a person birthdate. When I click on it a dialog fragment appears, but I have to click two times. After first click there is an keyboard and I don't want it.

I know that I can use button instead, but i prefer editext, because it looks better :) How can I change it? Can I open diagram in first edittext click? Thanks in advance!


Solution

  • You can make your EditText not focusable by adding android:focusable="false" in XML declaration and then use simple onClickListener on the EditText.

    Else, you can also use the following method to hide the keyboard:

    public void hideSoftKeyBoard(Activity activity) {
        // Check if no view has focus:
        View view = activity.getCurrentFocus();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
    

    This can be invoked as follows: hideSoftKeyBoard(this) from an activity or hideSoftKeyBoard(getActivity()) from a fragment.

    Hope this helps!