Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide soft Keyboard without focus in Android?

I meet this case, I check the soft keyboard is on and I want to dismiss in the code, when I use the below code it cannot dismiss the keyboard, because the code can not find any focus, but the keyboard is still on, so how can I hide it?

private void hideSoftKeyboard() {
    Activity activity = (Activity) sContext;
    View view = activity.getCurrentFocus();
    if (view != null) {

        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        //((Activity) sContext).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    } else {

            Log.i(sClassTag,"focus not found");

    }
}
like image 814
newszer Avatar asked Dec 20 '16 16:12

newszer


People also ask

How do I hide my soft keyboard?

Hiding the Soft Keyboard Programmatically You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.

How do I make my keyboard invisible on Android?

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases, you will want to pass in InputMethodManager.

How do I turn off keyboard focus on Android?

clearFocus() in its touch event listener. This will clear all the focus on any touched textview. Then I proceed to close the soft keyboard on screen.

How do I hide soft keyboard when EditText is focused?

setShowSoftInputOnFocus(false); to disable the software keyboard showing when the EditText is touched. the hideKeyboard(this); call in OnCreate to forcible hide the software keyboard.


1 Answers

Try to use this

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your focused view.

View view = this.getCurrentFocus();
if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

This will force the keyboard to be hidden in all situations. In some cases you will want to pass in InputMethodManager.HIDE_IMPLICIT_ONLY as the second parameter to ensure you only hide the keyboard when the user didn't explicitly force it to appear (by holding down menu).

or this

InputMethodManager imm =(InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);

you can find more details here

like image 134
Charuක Avatar answered Oct 16 '22 05:10

Charuක