Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep a ScrollView to be scrolled to the Bottom?

Tags:

java

android

I have a ScrollView with a LinearLayout inside an I'm adding TextViews. But I always want the last added TextView to be visible, so I need the ScrollView to scroll to the Bottom, when a TextView is added. I don't know why, but when I call scrollto(), scrollby() or fullScroll() after adding a textview, it only scrolls to the textview before the last one.

Do you know a better way to do this?

Thanks a lot!

Code:

I got a Button, which calls this function:

private void addRound() {
    // TODO Auto-generated method stub
    TextView newRound = new TextView(Stopwatch.this);
    newRound.setText("" + counter + ". - " + timerText());
    newRound.setTextSize(20);
    newRound.setGravity(Gravity.CENTER);
    linlay.addView(newRound);
    counter++;
}

After calling this function I call fullScroll().

addRound();
sv.fullScroll(View.FOCUS_DOWN);

sv ist my ScrollView, linlay is the linearlayout inside the scrollview.

like image 393
TheBrillowable Avatar asked Feb 06 '12 19:02

TheBrillowable


People also ask

How do I scroll to the bottom of the page on Android?

Make sure the webpage is long enough so that the browser shows a scroll bar. Once there, to get to the bottom of the page, simply tap the top right corner on your device. In the screenshot below, I need to tap the area where the time is shown. The page will automatically scroll all the way down to the bottom.

What is contentContainerStyle?

ScrollView contentContainerStyle defines the inner container of it, e.g items alignments, padding, etc. Follow this answer to receive notifications.

Which is better FlatList or ScrollView?

ScrollView renders all its react child components at once, but this has a performance downside. FlatList renders items lazily, when they are about to appear, and removes items that scroll way off-screen to save memory and processing time.


1 Answers

I reckon it's because the ScrollView is not quite updated by the time you call sv.scrollFull(View.FOCUS_DOWN); Try the following (to replace your second code sample):

addRound();
sv.post(new Runnable() {

   @Override
   public void run() {
     sv.fullScroll(View.FOCUS_DOWN);
   }
});

If the above doesn't work, try the following (it's not an elegant way of doing it but it may work):

addRound();
sv.postDelayed(new Runnable() {

   @Override
   public void run() {
     sv.fullScroll(View.FOCUS_DOWN);
   }
}, 100);

Hope this works!

like image 107
Henry Thompson Avatar answered Nov 14 '22 23:11

Henry Thompson