SoFunction
Updated on 2025-04-10

Android soft keyboard display mode and on and off mode (recommended)

Android soft keyboard display mode:

Android defines a property named windowSoftInputMode, which allows the program to control the way the active main window is adjusted. We can set the Activity in it. For example: android:windowSoftInputMode="stateUnchanged|adjustPan"

There are two parts of the optional value of this property, one is the state control of the soft keyboard, and the other is the adjustment of the active main window. This article will not be discussed in the previous part, please consult the Android documentation yourself.

Mode 1, compression mode

If the value of windowSoftInputMode is set to adjustResize, the main window of the Activity is always resized to leave space for the soft keyboard.

Let's test through a piece of code what the system does when the input method pops up after we set this property.

Mode 2, translation mode

If the value of windowSoftInputMode is set to adjustPan, then the main window of the Activity does not resize the screen to leave space for the soft keyboard. Instead, the content of the current window will automatically move so that the current focus is never overwritten by the keyboard and the user can always see the part of the input. This is usually not desirable than resize, as the user may turn off the soft keyboard in order to obtain interaction with the overwritten content.

In the above example, we change the property: android: windowSoftInputMode = "adjustPan"

Mode Three Automatic Mode

When the property windowSoftInputMode is set to adjustUspecified, it is not specified whether the Activity main window is resized to leave space for the soft keyboard, or whether the content on the window gets the current focus on the screen is visible. The system will automatically select one of these modes that depends primarily on whether the content of the window has any layout view that can scroll their content. If there is such a view, the window will be resized, assumptions that make the contents of the scroll window visible in a smaller area. This is the default behavior setting for the main window.

In other words, the system automatically decides whether to use translation mode or compression mode, and the decisive factor lies in whether the content can be scrolled.

Android soft keyboard open and close:

Switch soft keyboard:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
//Get the InputMethodManager instanceif (()) { 
//If enabled(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);  
//Close the soft keyboard, the opening method is the same. This method is to switch the opening and closing states.}

Close the soft keyboard

if(getCurrentFocus()!=null) 
      { 
        ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)) 
        .hideSoftInputFromWindow(getCurrentFocus() 
            .getWindowToken(), 
            InputMethodManager.HIDE_NOT_ALWAYS);  
      } 

The above is the Android soft keyboard display mode and on and off mode (recommended) introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!