Hide keyboard when displaying DialogFragment on tablet?

I use DialogFragment with ListView (list of all clients) and EditText (for searching from a list), it works fine. But whenever a dialog shows a fragment, the keyboard is always displayed, and the user must resign. Is there a way to hide this for the first time by showing a piece of dialogue? then when the user clicks on the text editing, a keyboard should appear.

I tried to set android:focusable="false" in my XML, but it always hides the keyboard after clicking on EditText also not displayed.

Then I tried to set android:focusableInTouchMode="true" , but getting the same as above

+9
android dialogfragment keyboard show-hide
source share
4 answers

In the DialogFragment onCreateView () dialog box, 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; } 
+21
source share

This should solve your problem.

 android:windowSoftInputMode="stateHidden" 

or

 android:windowSoftInputMode="stateUnchanged" 
+1
source share

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); } } 
0
source share

Case 1: if you want to close the keyboard when opening a 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 to close the keyboard when choosing auto-complete text or any other type of text editing, use a 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 it will work

0
source share

All Articles