Search code examples
androidandroid-linearlayoutandroid-scrollview

Set LinearLayout visible and call getBottom


In my app, I have a ScrollView with a LinearLayout whose visibility is set on GONE. I need to make it visible and then have my ScrollView scroll to the bottom of the LinearLayout. For this I'm using this code:

mLinearLayout.setVisibility(View.VISIBLE);
mScrollView.smoothScrollTo(0, mLinearLayout.getBottom());

This however, does not work. When the ScrollView is asked to scroll the LinearLayout still returns 0 on getBottom().

So when this is called for the first time, the LinearLayout is visible, but the scrollview has not scrolled. When it is called for a second time, it does scroll down to the right position.

How can I fix this?


Solution

  • You need to put your smmothScrollTo method inside a new thread like this:

         mScrollView.post(new Runnable() {
                    public void run() {
                        mScrollView.smoothScrollTo(0,mLinearLayout.getBottom());
    
                    }
                });