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

In 2016, these six industries will be completely reshuffled

In 2015, everyone started their own business, and...

Hong Raiders Trend Season Hunting Hunting C Intensive Training Camp

Hong Raiders Trend Season Hunting Hunting C Stren...

How to operate with millions of users?

Whether it is a website or an APP, when the numbe...

Apple's flaw: iPhone 6's charging efficiency is not as good as mid-range phones

As functions become increasingly complete, the ba...

Xigua Video Product Analysis Report

Watching videos in free time has become an entert...

2022 National Public Security Professional Knowledge Course

Course Catalog: ├──00 Electronic version of the b...

User operation: user growth in the post-traffic era!

The decline of traffic dividends is now an irreve...

Marketing analysis of Jay Chou’s first live broadcast on Kuaishou!

It took nearly two months from the official annou...

10 common problems faced by programmers

[[221521]] The most difficult task for programmer...