Scroll after view becomes visible

Issue

I am trying to scroll to the bottom of my scrollView after a view becomes visible with button click. The problem is the scrollTo function is applied before the view is actually visible. I know this because when the button is pressed twice, it scrolls to the bottom on the second click. So, is there a way to scroll after the view becomes visible?

button.setOnClickListener(v -> {
    constraintLayout.setVisibility(View.VISIBLE);
    scrollView.smoothScrollTo(0, constraintLayout.getBottom());
}

Solution

button.setOnClickListener(v -> {
    constraintLayout.setVisibility(View.VISIBLE);
    Handler handler  new Handler();
    handler.postDelayed(() -> {
        scrollView.smoothScrollTo(0, constraintLayout.getBottom());
    }, 100);
}

I just figured out this works, but I was hoping to not use a delay.

Answered By – tbear

Leave a Comment