android - EditText height resize based on keyboard
Categories:
Dynamically Resizing Android EditText Height with Keyboard Visibility

Learn how to make your Android EditText fields automatically adjust their height when the soft keyboard appears or disappears, ensuring a smooth user experience.
In Android development, managing UI elements in response to the soft keyboard's appearance is a common challenge. Specifically, an EditText
that needs to expand or shrink its height to accommodate the keyboard, without being obscured or leaving excessive empty space, requires careful handling. This article will guide you through the techniques to achieve dynamic EditText
height resizing, ensuring your application's layout remains fluid and user-friendly.
Understanding Keyboard Interaction and Window Adjustments
Android provides several ways to handle how your activity's window adjusts when the soft keyboard is shown. The primary mechanism is controlled by the android:windowSoftInputMode
attribute in your AndroidManifest.xml
file. Understanding the different modes is crucial for choosing the right approach to resize your EditText
.
When the keyboard appears, it can either resize your activity's window (adjustResize
) or pan the window (adjustPan
). For dynamic EditText
resizing, adjustResize
is generally preferred because it allows the layout to redraw and adjust its dimensions. If adjustPan
is used, the window simply slides up, potentially obscuring parts of your layout without resizing them.
flowchart TD A[Keyboard Appears] --> B{windowSoftInputMode?} B -->|adjustResize| C[Activity Window Resizes] C --> D[Layout Redraws] D --> E["EditText" Adjusts Height] B -->|adjustPan| F[Activity Window Pans Up] F --> G[Layout May Be Obscured] G --> H["EditText" Not Resized Automatically]
Flowchart of Android window adjustment behavior with soft keyboard
Implementing adjustResize
for Automatic Layout Adjustment
The simplest and often most effective way to handle EditText
resizing is to leverage the android:windowSoftInputMode="adjustResize"
setting. When this mode is active, the system automatically resizes the activity's main window to make room for the soft keyboard. If your EditText
is within a ConstraintLayout
, LinearLayout
, or RelativeLayout
that correctly uses match_parent
or wrap_content
and appropriate constraints/weights, it will often resize itself naturally.
However, for more complex layouts or when the EditText
is at the bottom of a ScrollView
or RecyclerView
, you might need additional logic to ensure it remains visible and correctly sized. The key is to ensure your layout hierarchy allows for vertical resizing.
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize">
<!-- Other activity attributes -->
</activity>
Setting android:windowSoftInputMode
in AndroidManifest.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Other content that might scroll -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!-- Content inside scroll view -->
</ScrollView>
<EditText
android:id="@+id/my_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type your message" />
</LinearLayout>
Example layout using LinearLayout
with adjustResize
adjustResize
, ensure your root layout is set to android:layout_height="match_parent"
. This allows the system to correctly calculate the available space after the keyboard appears and resize the layout accordingly.Handling Edge Cases and Manual Adjustments
While adjustResize
often works well, there are scenarios where you might need more granular control or a custom solution. For instance, if your EditText
is part of a Fragment
or a custom view, or if you need to animate the resizing, you might have to implement a listener for keyboard visibility changes.
One common approach involves listening to changes in the root view's height. When the keyboard appears, the root view's height (excluding the keyboard area) decreases. You can calculate the difference and adjust your EditText
's layout parameters or margins accordingly. This method requires careful calculation of the visible display frame.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rootView = findViewById<View>(android.R.id.content)
val editText = findViewById<EditText>(R.id.my_edit_text)
rootView.viewTreeObserver.addOnGlobalLayoutListener {
val r = Rect()
rootView.getWindowVisibleDisplayFrame(r)
val screenHeight = rootView.rootView.height
// r.bottom is the position above soft input keyboard or below status bar
// screenHeight is the total height of the screen
val keyboardHeight = screenHeight - r.bottom
if (keyboardHeight > screenHeight * 0.15) { // Keyboard is visible (threshold 15% of screen height)
// Keyboard is open, adjust EditText height/margin if needed
// Example: If EditText is inside a LinearLayout at the bottom
// You might adjust its layout_height or bottom margin
// For simple adjustResize, this might not be necessary
} else {
// Keyboard is closed
}
}
}
}
Listening for keyboard visibility changes to manually adjust layout
adjustResize
if it meets your requirements, as it's the system's intended way to handle this behavior and is generally more robust.