18

I'm developing a chat screen. It consists of CollapsingToolbarLayout for user profile image, RecyclerView for messages list and an edit box to send messages. I can't figure out if it's possible to pin edit box to the bottom screen and prevent it from scrolling with the rest of the screen?

I achieved almost what I want if I wrap CoordinatorLayout in a vertical LinearLayout and put EditText outside of CoordinatorLayout. However in this case keyboard behavior is detached from RecyclerView - it doesn't scroll up/down when you open keyboard.

If I try to put EditText inside CoordinatorLayout it scrolls out of screen, I don't know if there is any special Behavior I need to set for it

My Layout

<?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:id="@+id/keyboard_listener"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
>

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    >

    <RecyclerView
       android:id="@android:id/list"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:clickable="true"
       android:clipToPadding="false"
       android:focusableInTouchMode="true"
       android:paddingBottom="20dp"
       android:scrollbarStyle="outsideOverlay"
       android:scrollbars="vertical"
       android:transcriptMode="normal"
       app:layout_behavior="@string/appbar_scrolling_view_behavior"
       />

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/screen_toolbar_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            app:titleEnabled="false"
            >

            <FrameLayout
                android:id="@+id/toolbar_fragment_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />

            <Toolbar
                android:id="@+id/screen_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/transparent"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:layout_collapseMode="pin"
                />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

        <EditText
            android:id="@+id/messageEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@null"
            android:hint="@string/send_hint"
            android:inputType="textCapSentences|textMultiLine"
            android:maxLength="2000"
            android:maxLines="4"
            android:padding="10dp"
            android:textSize="14sp"/>

</LinearLayout>
Anton
  • 4,395
  • 7
  • 32
  • 46
  • well, if you really want to `EditText` at the bottom of screen you should exclude it from `CoordinatorLayout` and put it in your activity layout. The second one should be `RelativeLayout`, so you would be able to give an EditText attributes like `android:layout_alignParentBottom="true"` – piotrek1543 Dec 16 '15 at 23:05
  • 1
    @piotrek1543 i appreciate your efforts, but sorry - you are really not helping at all :) Please read my question once again - I did wrap CoordinatorLayout in a LinearLayout and my edittext is pinned just as I want it to. But then keyboard doesn't scroll the RecyclerView – Anton Dec 17 '15 at 01:04
  • 1
    In regards to your coordinatorlayout issue, android:isScrollContainer="true" will fix this, the name is somewhat misleading but the comments are the key: Set this if the view will serve as a scrolling container, meaning that it can be resized to shrink its overall window so that there will be space for an input method. If not set, the default value will be true if "scrollbars" has the vertical scrollbar set, else it will be false. – Ben Neill Feb 16 '16 at 00:54
  • Unfortunately, I don't have a solution to your problem, but your existing setup was good enough to solve my problem. Hope that's some consolation? – DataGraham Mar 23 '16 at 20:03
  • @AntonHave you found any solution? – Moorthy Feb 01 '18 at 11:26
  • I had same problem. This helped me https://stackoverflow.com/a/35405095/9586934 – Andrey Gritsay Aug 09 '19 at 05:52

1 Answers1

11

Add this in your EditText, my friend, just like a floatingActionButton

   app:layout_anchor="@id/your bottom view id"
   app:layout_anchorGravity="bottom|right|end"
Eric Zhang
  • 119
  • 1
  • 5