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

Tik Tok is popular, Yinyue is hot: How are Toutiao’s hit products created?

Popular products cannot be copied, but the logic ...

WeChat Reading - How to use social reading apps

From the perspective of event operation and user ...

Introduction and principle analysis of iOS automated testing framework Kiwi

This article is reprinted from the WeChat public ...

Can the self-produced talent show help Sohu Video regain its momentum?

At the beginning of 2015, Zhang Chaoyang, who ret...

More than 5,000 years ago, they lived in "three bedrooms and one living room"

On the banks of the great river, there are relics...

How to promote an H5 that cost 20,000 yuan?

Weibo, WeChat, H5, offline activities ... If you a...

Will free traffic service become the next “free lunch”?

Earlier this year, Dan Check, vice president of t...

Postpartum recovery and postpartum body shaping latest course Ganmin 16 courses

Introduction to the latest course of postpartum r...