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

If you want your product copy to speak well, first learn to cut the copy

A copywriter is a salesperson who sits behind a k...

Nature's GPS: How do animals use the natural world to navigate?

Millions of animals around the world follow the s...

Tips for advertising on Weibo, Zhihu and other platforms, with case analysis

Weibo is a platform with very high user activity,...

Predict, sense, respond: the Internet of Things for government

"Predict, Sense, Respond: Government Interne...

Who retires satellites? Space tugs!

In almost every harbor, we can see a strange kind...

Vitamin C is 21 times more than that of apples! This fruit is perfect for spring

There is a kind of fruit that “people who love it...

Short video promotion and operation: Who has better ability to bring goods?

Why do the giants all want to make short videos? ...

Activity Operation丨How to Play APP Online Activities and Offline Promotion

Today, I will summarize some methods of online ac...

Moments ads have been upgraded. Now you can chat with advertisers in Moments

Yesterday, a magical advertisement appeared on We...

5 types of win-win price discrimination, the driving force behind prices

In order to ensure that every consumer with deman...