1

i'm trying to prevent the keyboard from showing up after calling requestFocus() on a SearchView but there is no solution to fix that on Android 8.

i tried :

/*1/

 android:windowSoftInputMode="stateAlwaysHidden" 

/*2/

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

/*3/

InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); if (activity.getCurrentFocus() != null && activity.getCurrentFocus().getWindowToken() != null) inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0); 

/*4/

 final View activityRootView = findViewById(R.id.rootLayout); activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight(); if (heightDiff > dpToPx(Act_StoreOrder.this, 200)) { // if more than 200 dp, it's probably a keyboard... Log.i("<<<Clavier","Clavier Showed Up"); //hide here } } }); 

onGlobalLayout() worked but the softkeyboard show up for nearly 0.5s and disappear.

Any help to make that softkeyboard hidden even after calling requestFocus() ??

8
  • Does the device have a hardware keyboard? Why don't you want a keyboard when they tap on a field you type in? Commented Oct 11, 2018 at 14:29
  • because as you said i am using a hardware keyboard to fill that searchview ... Commented Oct 11, 2018 at 14:36
  • If you have an attached hardware keyboard, most software ones hide themselves. Commented Oct 11, 2018 at 14:49
  • Also, your on global layout code will fail if they use screen casting, picture in picture, or split screen apps. Or if they use a very small keyboard like minimum. Commented Oct 11, 2018 at 14:51
  • let's forget about the hardware keyboard , how to hide the softkeyboard if there is no hardkeyboard ?? Commented Oct 11, 2018 at 14:55

1 Answer 1

1

finally i solved the problem, i just used that function bellow to intercept FocusChanges and hide the softKeyboard after 100 (enought time to intercept the softkeyboard)

 @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { View lFocused = getCurrentFocus(); if (lFocused != null) lFocused.postDelayed(new Runnable() { @Override public void run() { InputMethodManager lInputManager = (InputMethodManager) pContext.getSystemService(Context.INPUT_METHOD_SERVICE); lInputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); } }, 100);//Modified to 100ms to intercept SoftKeyBoard on Android 8 (Oreo) and hide it. } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.