1

I have a ScrollView with a lot of layouts inside that appears step by step (button 1 shows layout 1, ...). And, as a final step, my last button shows a footer.

At each appearance of a layout, I want the scrollview to scroll to the bottom. Same when my footer appear.

I tried solutions posted on Stack but they do not work for me... : https://stackoverflow.com/a/34866634/11656272 or binding.scroll.scrollTo(0, Int.MAX_VALUE)

I also try with "findViewById" instead of binding...

Any idea?

layout file

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

   <RelativeLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_gravity="center_horizontal"
   android:layout_margin="10dp">

   <!-- scrollView -->
   <ScrollView
      android:id="@+id/scroll"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_above="@id/footerLayout">

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

      <Button
         android:id="@+id/btn1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button 1" />

      <LinearLayout
         android:id="@+id/layout1"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical"
         android:visibility="gone">
         <!-- ... -->
      </LinearLayout>

      <!-- same for btn2, layout2, ... -->

      <LinearLayout
         android:id="@+id/layoutLast"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical"
         android:visibility="gone">

         <Button
         android:id="@+id/btnLast"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Last button" />
      </LinearLayout>
      </LinearLayout>
   </ScrollView>

   <!-- footer layout -->
   <LinearLayout
      android:id="@+id/footerLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:orientation="vertical"
      android:visibility="gone">

      <!--- ... --->
   </LinearLayout>
   <!-- footer layout - END -->
   </RelativeLayout>
</layout>

kt file

package com.example.myapp.controller


import ...

class MainActivity : AppCompatActivity() {

   fun ScrollView.scrollToBottom() {
      val lastChild = getChildAt(childCount - 1)
      val bottom = lastChild.bottom + paddingBottom
      val delta = bottom - (scrollY + height)
      smoothScrollBy(0, delta)
   }

   private lateinit var binding: com.example.myapp.databinding.ActivityMainBinding

   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

      // *** buttons 1, 2, 3, ... show layout1, layout2, ...
      binding.btn1.setOnClickListener {_ ->
         binding.layout1.visibility = VISIBLE

         binding.scroll.scrollToBottom()
      }

      // ... etc for other layouts

      // *** Main button -> show footer layout and scroll down ***
      binding.btnLast.setOnClickListener {_ ->
         binding.footerLayout.visibility = VISIBLE

         binding.scroll.scrollToBottom()
      }
   }
}

1 Answer 1

6

Try using scroll.fullScroll

scroll.post { 
    scroll.fullScroll(View.FOCUS_DOWN) 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Is it possible to scroll with an animation? this code is immediately takes us to bottom without any scroll gesture

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.