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

Android Annotations Quick Start and Practical Analysis

First of all, what is an annotation? @Override is...

Practical review: How to cold start community fission?

Let me begin by saying something, a very honest t...

How to attract user traffic and trigger social communication?

Today’s topic is how to ignite social communicati...

Summary of AndroidStudio shortcut keys

Ctrl+G / Ctrl+Alt+Shift+G: Query where a variable...

Giant planets outside the solar system hold many mysteries

In science fiction works, aliens with tentacles d...

User operation: How to increase user growth?

The topic of user growth has been the most discus...

Is it easier to gain weight in summer? 8 ways to stay away from fat

A woman called me and asked me: Other people gain...

How to SEO a website? A plan to increase website rights by 6 per month!

The good or bad ranking of a website is not due t...

OPPO R9 from the perspective of its frame: An Android phone reshaping the iPhone

In an era of highly homogenized smartphone hardwa...

Why don’t young people like to buy new mobile phones?

"Hold on to 17,000, it can't go any lowe...

No need for Root, you can also use the powerful Xposed framework: VirtualXposed

If you like to tinker with Android devices, you s...

The most powerful method to obtain seed users for App promotion!

No matter how cool the concept of a product is or...