Search code examples
javaandroidlistviewscrolltextview

Android - TextView scroll with / before Listview


I'm working on a chat application and I want to show a message when the user scroll until the top of the messages list.

Here's a screen of my activity:

Current appearance

The red rectangle is the ListView of messages and the green one is the message (TextView) I want to show only before the first item of the list.

Now the TextView is in the XML layout so it "cover" the ListView (that is, I want it to disappear when the ListView scrolls).

Here is my 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:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="fr.uha.tenich.talk2me.MainActivity"
    tools:showIn="@layout/app_bar_main"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Vous avez atteint le début de ce salon"
        android:textAppearance="@android:style/TextAppearance.Material.Medium"
        android:textAlignment="center"
        android:textColor="@android:color/darker_gray"
        android:gravity="center_vertical"
        android:background="@android:color/white"
        android:id="@+id/debut_salon"/>

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

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/current_channel_messages"
            android:fillViewport="true"
            android:divider="@android:color/white"
            android:dividerHeight="1dp"
            android:background="@android:color/white"
            android:listSelector="@android:color/transparent" />

    </LinearLayout>

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@android:color/black"/>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:gravity="bottom"
            android:background="@android:color/white">

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:hint="Tapez votre message ici"
                android:ems="10"
                android:id="@+id/message_input"
                android:layout_weight="3"
                android:backgroundTint="@android:color/transparent" />

            <ImageButton
                app:srcCompat="@drawable/ic_menu_send"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:layout_width="wrap_content"
                android:background="@android:color/white"
                android:onClick="onSendMessageClick" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

How can I make the TextView "scroll" with the ListView?


Solution

  • Just add a header view to the listview and that will solve your problem