title: How to programmatically scroll to bottom (Android, kotlin)?
body: <p>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.</p>
  
  <p>At each appearance of a layout, I want the scrollview to scroll to the bottom.
  Same when my footer appear.</p>
  
  <p>I tried solutions posted on Stack but they do not work for me... :
  <a href="https://stackoverflow.com/a/34866634/11656272">https://stackoverflow.com/a/34866634/11656272</a>
  or <code>binding.scroll.scrollTo(0, Int.MAX_VALUE)</code></p>
  
  <p>I also try with "findViewById" instead of binding...</p>
  
  <p>Any idea?</p>
  
  <p><strong>layout file</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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"&gt;
  
     &lt;RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_gravity="center_horizontal"
     android:layout_margin="10dp"&gt;
  
     &lt;!-- scrollView --&gt;
     &lt;ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/footerLayout"&gt;
  
        &lt;LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"&gt;
  
        &lt;Button
           android:id="@+id/btn1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Button 1" /&gt;
  
        &lt;LinearLayout
           android:id="@+id/layout1"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical"
           android:visibility="gone"&gt;
           &lt;!-- ... --&gt;
        &lt;/LinearLayout&gt;
  
        &lt;!-- same for btn2, layout2, ... --&gt;
  
        &lt;LinearLayout
           android:id="@+id/layoutLast"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical"
           android:visibility="gone"&gt;
  
           &lt;Button
           android:id="@+id/btnLast"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Last button" /&gt;
        &lt;/LinearLayout&gt;
        &lt;/LinearLayout&gt;
     &lt;/ScrollView&gt;
  
     &lt;!-- footer layout --&gt;
     &lt;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"&gt;
  
        &lt;!--- ... ---&gt;
     &lt;/LinearLayout&gt;
     &lt;!-- footer layout - END --&gt;
     &lt;/RelativeLayout&gt;
  &lt;/layout&gt;
  </code></pre>
  
  <p><strong><em>kt file</em></strong></p>
  
  <pre><code>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 {_ -&gt;
           binding.layout1.visibility = VISIBLE
  
           binding.scroll.scrollToBottom()
        }
  
        // ... etc for other layouts
  
        // *** Main button -&gt; show footer layout and scroll down ***
        binding.btnLast.setOnClickListener {_ -&gt;
           binding.footerLayout.visibility = VISIBLE
  
           binding.scroll.scrollToBottom()
        }
     }
  }
  </code></pre>