0

I want to scroll ScrollView programmatically but i can't.

None of ScrollView methods "scrollTo, arrowScroll, pageScroll and fullScroll" doesn't work!

final ScrollView scrollView = (ScrollView) view.findViewById(R.id.scrollbar_textchat);
Button sendButton = (Button) view.findViewById(R.id.btn_send);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollView.fullScroll(View.FOCUS_DOWN);
            }
        });

xml layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!--main-->
        <ScrollView
            android:id="@+id/scrollbar_textchat"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:verticalScrollbarPosition="left"
            android:fillViewport="true">
                <!--messages list-->
                <ListView
                    android:id="@+id/list_textchat"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:listSelector="@android:color/transparent"
                    android:cacheColorHint="@android:color/transparent" />
        </ScrollView>
        <!--/main-->

        <!--line-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_height="2dp"
            android:background="#FFEEEEEE"/>

        <!--already Typing message...-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:orientation="horizontal">

            <com.salamatyar.components.TypingAnimationView
                android:id="@+id/typing_textchat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"/>

            <TextView
                android:id="@+id/text_typing_message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="typing" />

        </LinearLayout>
        <!--/already Typing message...-->

        <!--line-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_height="2dp"
            android:background="#FFEEEEEE"/>

        <!--type-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <EditText
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <Button
                android:id="@+id/btn_send"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="send"/>

        </LinearLayout>
        <!--/type-->
    </LinearLayout>

</LinearLayout>

3 Answers 3

1

try this code:

 final ScrollView scrollView = (ScrollView) view.findViewById(R.id.scrollbar_textchat);
scrollView.post(new Runnable() { 
        public void run() { 
             scrollView.scrollTo(0, scrollView.getBottom());
        } 
});

use ListView: to scroll to bottom poistion

  listView.post(new Runnable(){
  public void run() {
    listView.setSelection(listView.getCount() - 1);
  }});
Sign up to request clarification or add additional context in comments.

3 Comments

You are adding scrollview to Listview Only. ..so try scrolling the listview instead..check the edit
Also another query: did ur manual scroll works on listview items?
thank you very much, i scrolled Listview and did worked.
1

xml layout:

remove

 <ScrollView
        android:id="@+id/scrollbar_textchat"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:verticalScrollbarPosition="left"
        android:fillViewport="true">
          -------------------------------
    </ScrollView>

and use android:layout_weight="1"

       <ListView
            android:id="@+id/list_textchat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:listSelector="@android:color/transparent"
            android:cacheColorHint="@android:color/transparent" />

Comments

0

you should run the code inside the scrollView.post like this:

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

2 Comments

thank you for your answer, but i tested this before and didn't work!
that is strange! this is always works for me, any way, did you tried scrollView.postDelayed instead of scrollView.post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.