Search code examples
androidkeyboardshow-hidedialogfragment

Hide keyboard when showing DialogFragment on tablet?


I am using DialogFragment with ListView (to list all customer) and EditText (to search from list), it's working fine. But, whenever the dialog shows from the fragment, the keyboard is always shown and the user needs to resign. Is there any way to hide this at the first time while showing the dialog fragment? then, when the user clicks on edit text, the keyboard should appear.

I have tried setting android:focusable="false" in my XML but, it always hides the keyboard after click on EditText also not showing.

Then I tried setting android:focusableInTouchMode="true" but, getting same as above


Solution

  • In your DialogFragment onCreateView() add the following:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState)
    {
     View view = super.onCreateView( inflater, container, savedInstanceState );
     //to hide keyboard when showing dialog fragment
     getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
     return view;
    }