Search code examples
androidandroid-studiokeyboardscrollview

Android Studio - Scrollview moves outside the screen on keyboard open


I want to make a chat screen which has an EditText and a Button at the bottom (wrapped in a LinearLayout), and a ScrollView for the messages at the top. The problem is that when the user taps the EditText and the keyboard opens, the ScrollView will go off screen as a result of moving up. I would like to move the Layout (with the EditBox and the Button) above the keyboard and the ScrollView to be resized to fit the screen. The problem is shown on the pictures below:

enter image description here

As you can see, the line that writes "Chat" (left image) has disappeared in the right image.

I use this XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:padding="15sp"
    android:orientation="vertical"
    android:background="#ccdcf7"
    tools:context="com.orionz.sockettest.ChatActivity">

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="15"
        android:isScrollContainer="false">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/chatBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="Chat\n"
                android:textColor="#000000"
                android:textIsSelectable="true"
                android:textSize="22dp" />
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center|bottom"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/msgInput"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.8"
            android:ems="10"
            android:hint="Message..."
            android:inputType="text" />

        <Button
            android:id="@+id/sendButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:background="@drawable/mybutton"
            android:padding="5sp"
            android:text="Send"
            android:textColor="#ffffff"
            android:textSize="20sp" />

    </LinearLayout>

</LinearLayout>

I have seen many answers such as setting the android:windowSoftInputMode but none has worked. I use Android Studio. Any help would be appreciated.


Solution

  • Finally, I found what was wrong in my case.

    • The theme was full screen. In the style of the activity I had this line that caused the problem:

    <item name="android:windowFullscreen">true</item>

    • In the java code, I gave a command to open the keyboard on the start of the activity:

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

    which was also part of the problem

    • After adding the adjustResize property to the activity in the manifest, everything worked fine:

    android:windowSoftInputMode="adjustResize"