0

I have created a chatroom where user can discuss on several Topic. I'm using a Scrollview inside RelativeLayout which contains chatConversation The Porblem is when user opens app then he has to scroll to the Bottom HImself to chat or view the new chat discussion.

How can I force scrollview to start at Bottom when activity is started..

enter image description here

Below is my layout file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@style/AppTheme.NoActionBar"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.Toolbar
        android:id="@+id/main_chat"
        app:titleTextAppearance="@style/Toolbar.TitleText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#F0791F"
        android:theme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navigationIcon="@drawable/ic_people_outline_black_24dp"/>


    <Button
        android:id="@+id/btn_send"
        android:layout_width="42dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:background="@android:color/transparent"
        android:drawableLeft="@drawable/ic_send_black_24dp" />

    <EditText
        android:id="@+id/msg_input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@+id/btn_send"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/btn_send"
        android:layout_toStartOf="@+id/btn_send"
        android:background="@null"
        android:hint="Write a message" />

    <ScrollView
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView"
        android:transcriptMode="alwaysScroll"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/btn_send"
        android:layout_below="@+id/main_chat"
        android:layout_alignRight="@+id/btn_send"
        android:layout_alignEnd="@+id/btn_send">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:layout_marginTop="2dp"
            android:layout_weight="2"
            android:autoLink="all" />
    </ScrollView>
    <View
        android:layout_above="@+id/msg_input"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@android:color/darker_gray"/>
</RelativeLayout>

I already tried using

final ScrollView scrollLayout = ((ScrollView) findViewById(R.id.scrollView));

scrollLayout.post(new Runnable() {
    @Override
    public void run() {
        scrollLayout.fullScroll(ScrollView.FOCUS_DOWN);
    }
});

But it doesn't work either.

Please Help

Bir Nepali
  • 429
  • 1
  • 8
  • 20
  • 1
    I recommend using a RecyclerView with an item for each message, instead of one single TextView. You'll use fewer resources, and you can simply set it to start at the end by default https://stackoverflow.com/questions/26580723/how-to-scroll-to-the-bottom-of-a-recyclerview-scrolltoposition-doesnt-work – TheWanderer Nov 09 '18 at 02:14

2 Answers2

0

How are you displaying the messages? With a While Loop?

Last line of the while loop should be...

    mScrollView.fullScroll(ScrollView.FOCUS_DOWN);

mScrollView or whatever name you gave to the ScrollView

0

I think you're appending the messages in a single TextView (correct me if I'm wrong). Check if you are invoking fullScroll() after you are finished appending all the text in your TextView.

for (String oneMessage : messageList){
     myTextView.append(oneMessage);
     myTextView.append("\n");
     ....
     ....
}
//Scroll to bottom
scrollLayout.post(new Runnable() {
     @Override
     public void run() {
            boolean success = scrollLayout.fullScroll(ScrollView.FOCUS_DOWN);
            Log.d("Auto Scroll", success ? "Done" : "Failed"); 
     }
});

Let me know if this fixed your issue!

Prasad Pawar
  • 1,606
  • 1
  • 15
  • 30