The key to adjusting the Android window soft keyboard: windowSoftInputMode property setting

The key to adjusting the Android window soft keyboard: windowSoftInputMode property setting

windowSoftInputMode is an attribute in Android that defines how the activity window should adjust when the screen has focus and the soft keyboard (i.e. the on-screen keyboard) needs to be displayed. The attribute is located in the AndroidManifest.xml file and is set for each <activity> tag.

 <activity android:name=".MainActivity" android:windowSoftInputMode="stateHidden|adjustResize"> </activity>

windowSoftInputMode has multiple possible values, which can be divided into two categories: state-prefixed values ​​and adjust-prefixed values.

  1. 「state prefix value」:

stateUnspecified: The default state of the soft keyboard is determined by the system.

stateUnchanged: The soft keyboard will remain in its last state, whether visible or hidden.

stateHidden: When the Activity is created, the soft keyboard is hidden.

stateAlwaysHidden: The soft keyboard is always hidden, even if the user selects a field that requires text entry.

stateVisible: When the Activity is created, the soft keyboard is visible.

stateAlwaysVisible: The soft keyboard is always visible.

  1. 「adjust prefix value」:
  • adjustUnspecified: The default adjustment method. The system's choice depends on the screen size and whether the window is resizable.

  • adjustResize: When the soft keyboard is shown, the window is resized to make room for the soft keyboard. This usually means that the contents of the window are moved up to make room for the keyboard.

  • adjustPan: The window contents are not resized but are panned upward so that the currently focused field is not obscured by the keyboard.

Set in AndroidManifest.xml:

 <activity android:name=".MainActivity" android:windowSoftInputMode="stateHidden|adjustResize"> </activity>

Set in code:

 public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); } }

These values ​​can be combined by separating them with the "|" symbol. In the above code, stateHidden and adjustResize are combined together.

Common properties of windowSoftInputMode are adjustPan, adjustResize, adjustNothing

adjustPan

The window content will not be resized, but will be translated upward so that the currently focused field is not blocked by the keyboard. The size of the Activity window (DecorView) remains unchanged. When the focused EditText is located at the bottom of the screen and the soft keyboard pops up to block the EditText, the entire DecorView will move up, but how much it will move up is uncertain. Generally, it will move up until the EditText is just not blocked by the soft keyboard.

picture

adjustResize

When the soft keyboard is displayed, the window will be resized to make room for the soft keyboard. This usually means that the content of the window will move up to make room for the keyboard. The size of the DecorView will not change, and the content area contentView (id = android.R.content) will shrink accordingly to make room for the keyboard.

picture

Note: adjustResize only adjusts the size of contentView, so it is still possible to cover the EditText.

adjustNothing

The Activity window will not be resized and the contentView will not change size.

<<:  Common commands of the Android system tool dumpsys, effectively obtain device information and discover application crash problems

>>:  Seven attributes of Android Intent, the key to building efficient communication between applications

Recommend

How to collect UGC content through event promotion operations?

1. How to increase the quantity and conversion ra...

Refined user growth case!

ABtest is gaining more and more attention. Fast a...

Samsung and Apple mobile phone sales comparison

Samsung Electronics' Galaxy S5 flagship smartp...

Why did eBay return to China together with JD.com?

[[132426]] After a year of preparation, JD.com...

The latest iOS version has a fatal bug, and the iPhone can crash in three steps

If you are a veteran gamer, do you remember the b...

“Ancient Chinese buildings do not use a single nail”, is this true?

When visiting ancient buildings, you will often h...

Methanogens: Man's Good Friend

On a sunny afternoon, the editor took the childre...