1

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.

Thanasis1101
  • 1,614
  • 4
  • 17
  • 28

3 Answers3

1

Try to replace the ScrollView with a ListView like this :

<ListView
    android:id="@+id/list_view"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1">
</ListView>

store all the messages in an ArrayList, and set an adapter to the ListView you made :

ListView listView=(ListView) findViewById(R.id.list_view);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ArrayList);
listView.setAdapter(adapter);

Finally, add a listenner on your Button writing these lines :

EditText editText= (EditText) findViewById(R.id.msgInput);
ArrayList.add(editText.getText().toString());
adapter.notifyDataSetChanged();
S.Voulgaris
  • 184
  • 1
  • 4
0

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"

Thanasis1101
  • 1,614
  • 4
  • 17
  • 28
-1

Wrap Scrollview to the whole layout.

i.e place editText and button inside Scrollview and set scrollView Height to android:layout_height="match_parent"

kamranbhatti585
  • 232
  • 2
  • 16
  • I tried to wrap it all in a ScrollView but the result wasn't what I wanted. If you have done it and it works please post the xml code or describe the layouts in detail. – Thanasis1101 Dec 03 '17 at 15:59