183

I tried to write a code which is used to re-size the UI components when soft-keyboard appears. When I use adjustResize, it res-size the UI components and at the same time adjustPan gave me same output. I want to know the difference between them and when to use each component? Which one(adjustPan or adjustResize) is good for resizing UI?

Here is my xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/editText5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="45dp"
                android:ems="10"
                android:inputType="textPersonName" />

            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="40dp"
                android:text="My Button" />
        </LinearLayout>
    </RelativeLayout>

</ScrollView>

and the menifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.adjustscroll"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.adjustscroll.MainActivity"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan|adjustResize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
androidcodehunter
  • 21,567
  • 19
  • 47
  • 70
  • 1
    For those working in ReactNative (and probably native Android) - don't forget to rebuild the app when changing the AndroidMainfest file. – Filip Savic Feb 05 '21 at 10:45

6 Answers6

292

From the Android Developer Site link

"adjustResize"

The activity's main window is always resized to make room for the soft keyboard on screen.

"adjustPan"

The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

according to your comment, use following in your activity manifest

<activity android:windowSoftInputMode="adjustResize"> </activity>
Community
  • 1
  • 1
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • 2
    thank you for your nice reply. Now I am having a problem with my UI. I have a edittext and a button at the bottom of my layout. I want to show those above the soft-keyboard when soft-keyboard appears. How can I achieve this. Can you help me to solve this problem? – androidcodehunter Jul 01 '13 at 17:54
  • 2
    This is not working for me. Actually what I wanted: I just want to show Edittext and Button above the soft-keyboard when I try to write something in Edittext field. I tried your solution but not working. – androidcodehunter Jul 01 '13 at 18:00
  • oops .. it worked for me .. anyways .. is it possible to use scrollview? – stinepike Jul 01 '13 at 18:03
  • Now I am testing code in eclipse using my symphony phone. I have used scrollview but not working for me. – androidcodehunter Jul 01 '13 at 18:07
  • it would be more clear if you post the manifest xml .. and the layout xml – stinepike Jul 01 '13 at 18:08
  • I added my xml and menifest file. Please help me. – androidcodehunter Jul 01 '13 at 18:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32696/discussion-between-sharifur-rahman-and-stinepike) – androidcodehunter Jul 01 '13 at 18:17
  • hi Sharifur please check using adjustResize only .. updated the answer – stinepike Jul 01 '13 at 18:22
  • 2
    adjustResize only provide solution when UI is available for re-sizing but if I put 5 more edittext then it cannot works. Is it possible to show button and edittext above the soft-keyboard when UI is not available to re-size. Actually question is arising when I testing this problem. – androidcodehunter Jul 01 '13 at 18:31
  • ~"This is generally less desirable than resizing". Well, not if resizing causes your views to overlap on each other. Like when you have a tabbed navigation, and a bunch of views in each tab fragment - on some devices adjustResize may cause the views to overlap when the keyboard is shown. There, unfortunately, you have to set adjustPan if you want to prevent this from happening. – IgorGanapolsky Nov 11 '13 at 18:42
  • You should create scrollview as a root layout of your view where keyboard will be shown and replace adjustResize to adjustPan – support_ms Jun 19 '14 at 05:08
  • 1
    @support_ms Why ~"replace adjustResize to adjustPan" is necessary? – IgorGanapolsky Jun 09 '15 at 17:29
  • @IgorGanapolsky I don't remember that. I answered, when I was also working with that kind of task – support_ms Jun 10 '15 at 11:47
  • I don't think adjustPan is required for ScrollView. – Mark13426 Oct 04 '16 at 18:45
  • When I use adjustResize with a Material BottomSheetBehavior layout with a peekHeight of 50dp the keyboard ends up covering the BottomSheetBehaviour , when I use peekHeight of auto it works as desired but the whole BottomSheetBehavior layout is shown which is not the desired effect I want. When using adjustPan the effect is as desired but part of the BottomSheetBehavior layout is hidded under the keyboard. How can I fix it? I have researched a lot of question but still no solution. – Victor Okech Mar 12 '21 at 08:08
63

I was also a bit confused between adjustResize and adjustPan when I was a beginner. The definitions given above are correct.
AdjustResize : Main activity's content is resized to make room for soft input i.e keyboard
AdjustPan : Instead of resizing overall contents of the window, it only pans the content so that the user can always see what is he typing
AdjustNothing : As the name suggests nothing is resized or panned. Keyboard is opened as it is irrespective of whether it is hiding the contents or not.

I have a created a example for better understanding
Below is my xml file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:hint="Type Here"
        app:layout_constraintTop_toBottomOf="@id/button1"/>


    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/button2"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginBottom="@dimen/margin70dp"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@id/button1"
        app:layout_constraintEnd_toStartOf="@id/button3"
        android:layout_marginBottom="@dimen/margin70dp"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button3"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/button2"
        android:layout_marginBottom="@dimen/margin70dp"/>
</android.support.constraint.ConstraintLayout>

Here is the design view of the xml
original view

AdjustResize Example below:
adjustResize example

AdjustPan Example below:
adjustPan example

AdjustNothing Example below:
adjustNothing example

luke77
  • 2,255
  • 2
  • 18
  • 30
Nadeem Shaikh
  • 1,160
  • 9
  • 17
53

adjustResize = resize the page content

adjustPan = move page content without resizing page content

Jayakrishnan
  • 4,457
  • 29
  • 29
21

Thanks @Nadeem for the inspiration.

This may demonstrate the difference more intuitively.

When the keyboard is not showing:

noKeyboard

When the soft keyboard shows

Compare differences

On my simulator, adjustUnspecified has the same result with adjustPan.

Here is the layout xml file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!-- This layout takes all the reset of the screen -->
    <LinearLayout
        android:id="@+id/layout_colors"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/et_input_field"
        >
        <!-- Divide the layout into 3 different colors -->
        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@android:color/holo_red_light"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@android:color/holo_green_light"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@android:color/holo_blue_light"
            />
    </LinearLayout>

    <EditText
        android:id="@+id/et_input_field"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:imeOptions="actionDone"
        app:layout_constraintBottom_toBottomOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

Chandler
  • 3,061
  • 3
  • 22
  • 30
12

As doc says also keep in mind the correct value combination:

The setting must be one of the values listed in the following table, or a combination of one "state..." value plus one "adjust..." value. Setting multiple values in either group — multiple "state..." values, for example — has undefined results. Individual values are separated by a vertical bar (|). For example:

<activity android:windowSoftInputMode="stateVisible|adjustResize" . . . >
Tomask
  • 2,344
  • 3
  • 27
  • 37
8

You can use android:windowSoftInputMode="stateAlwaysHidden|adjustResize" in AndroidManifest.xml for your current activity, and use android:fitsSystemWindows="true" in styles or rootLayout.

TTKatrina
  • 466
  • 5
  • 5