Hide keyboard when showing DialogFragment on tablet?
Asked Answered
S

4

10

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

Serow answered 30/8, 2013 at 10:42 Comment(3)
Have a look at this postConduction
#1555609Cynthia
thanks friends will check it and let u know @GaneshSerow
S
22

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;
}
Serow answered 12/9, 2013 at 8:52 Comment(2)
Awesome. I had seen all the other posts that just dealt with activities and no matter where I put it when dealing with fragments it did not work.. getDialog() vs getActivity() was the key for me. Thanks!Wound
It doesn't work when the dialog is a DataPickerFragment in Android 4.4, and I put the hide keyboard method in the onResume() method and it works. Anyway, thx.Mabe
M
2

This should solve your problem

android:windowSoftInputMode="stateHidden"

or

android:windowSoftInputMode="stateUnchanged" 
Macleod answered 30/8, 2013 at 11:14 Comment(0)
S
0

use this method, it works for me:

public void hideSoftKeyboard() {
        try {
            View windowToken = getDialog().getWindow().getDecorView().getRootView();
            InputMethodManager imm = (InputMethodManager) getDialog().getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow( windowToken.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception ex) {
            Log.e(ex);
        }
    }
Skim answered 15/3, 2019 at 9:33 Comment(0)
T
0

Case 1:If you want close keyboard on open of dialog fragment

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;
}

case 2:If you want close keyboard on selection autocompletetext or any other edit text view use simple

 public static void hideDialogFragmentKeyboard(Context context,View view) {
        view.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        }, 100);
    }   

I think this will work

Theriot answered 12/6, 2019 at 6:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.