1

I am very new to using ConstraintLayout. In trying to learn to use this, I decided to make a simple chat app for myself.

I have a RecyclerView to show chat list and a ConstraintLayout under it for typing in my message (my code is at the bottom). I would very much want to use "fill-parent" attribute but I found that it's deprecated.

So I have set my RecyclerView to be constrained at the bottom by the ConstraintLayout below it. From this I am expecting the RecyclerView to "shrink" when the keyboard comes up to type my message in. However, it is not doing so, but rather stays at its full height.

Are there ways to resolve this?

Here's my code :

<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"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    tools:context=".StreamPlayer.StreamPlayerActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@id/ChatRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toTopOf="@id/ChatTypingLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:listitem="@layout/chat_layout" /

    <android.support.constraint.ConstraintLayout
        android:id="@id/ChatTypingLayout"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

    <EditText
        android:id="@id/ChatNameEditText"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:hint="@string/ChatNameEditText"
        android:inputType="text"
        android:maxLines="1"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/ChatMessageEditText"
        app:layout_constraintStart_toStartOf="parent"
        android:textSize="10dp"/>

    <EditText
        android:id="@id/ChatMessageEditText"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:hint="@string/ChatMessageEditText"
        android:inputType="textLongMessage"
        android:maxLines="3"
        app:layout_constraintHorizontal_weight="4"
        app:layout_constraintLeft_toRightOf="@id/ChatNameEditText"
        app:layout_constraintRight_toLeftOf="@id/ChatSendButton"
        android:textSize="10dp"/>

    <Button
        android:id="@id/ChatSendButton"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text="@string/ChatSendButton"
        app:layout_constraintHorizontal_weight="2"
        app:layout_constraintLeft_toRightOf="@id/ChatMessageEditText"
        app:layout_constraintRight_toRightOf="parent"/>
    </android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

This is when the keyboard has not come up Keyboard not open

This happens when the keyboard comes up Keyboard open

Thanks in advance

ohnu93
  • 263
  • 6
  • 22

2 Answers2

1

Check out this article on the Android developers' site which describes how the framework handles the soft keyboard appearing.

The android:windowSoftInputMode attribute can be used to specify what happens on a per-activity basis: whether the layout is resized or whether it scrolls etc.

Answer from Christopher Orr

You can also try this in your onCreate() method:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Matthew Mitchell
  • 514
  • 4
  • 14
  • Thank you for the answer! I've added this code snippet to my app but still no luck :S I think my RecyclerView is getting a fixed height in onCreate() even though I've not given it a fixed height in code nor in layout file (although I have given it 0dp in layout file). Any other ideas on how to resolve this issue? :S – ohnu93 Nov 03 '18 at 12:52
0

I think you should consider taking the second ConstraintLayout and making everything in one, that is the main purpose of the ConstraintLayout, avoiding nested layouts. And, how are you initializing your recycler view in code? are you setting a fixed height?

  • No I am not giving my RecyclerView a fixed height. So the whole point behind moving onto using ConstraintLayout is to get rid of nested layouts so then in this case I'll have to set the bottom portion(where I type in the chat) to be constraint at top by the bottom of the recyclerview and left and right to each other? – ohnu93 Nov 02 '18 at 02:54
  • I've tried this but still no luck :S it seems like the RecyclerView is filling up the parent other than the bottom portion(where I type in the chat) as I set its height to 0dp and given a fixed height to the bottom portion. The problem is that the RecyclerView's height will not change even when the softkey opens. Do you have any ideas why this may be the case? – ohnu93 Nov 03 '18 at 12:51