How to scroll to the bottom of ListView programmatically?
Asked Answered
B

3

49

After calling notifydatasetchanged(); I want to scroll to the bottom of list so that user see the last record in the Listview.

(I am writing Chat module so for that purpose I need latest record at the bottom of list to be visible)

Can any one guide me how to achieve this?

Barstow answered 29/3, 2010 at 9:56 Comment(0)
M
77

Try

listView.post(new Runnable(){
  public void run() {
    listView.setSelection(listView.getCount() - 1);
  }});

The 'post' seems to be required sometime in my experience, particularly if you have very recently updated the list.

Midshipman answered 29/3, 2010 at 10:10 Comment(4)
this works great. I tried android:stackFromBottom="true" before and the problem then is that if there was only a single element in my list view that it would be a the bottom of the screen. ThanksRentroll
Note: if the height of the last element is bigger than the height of the listview, the top of the last element will be aligned among the top of the list, i.e. the bottom of the element wont be visible. That's why the stackFromBottom solution is described by Ribose is better for chat-like functionality.Lucy
Worked like a charm! I also tried stackFromBottom="true" but since I was not clearing the list it didn't work for my situation. (At least I think that is why.)Knickknack
You're right, the post is necessary if this is called directly after notifyDataSetChanged(). Using post postpones the setSelection() until the update/refresh of the list is completed.Crete
S
83

I know that this has been answered and you took the answer and it was more than a year ago. But a better way to do this is Transcript Mode. For a demo, see the Android API Demo under Views > Lists > Transcript.

You would set the following on your list view in the XML.

android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"

It will always work whenever you call notifyDataSetChanged(). You can set android:transcriptMode to normal instead if you want an even better result for chat applications: it will scroll to the bottom only if the last item was already in view. That way your users can view the previous chat without interruption when other users chat.

Sandon answered 10/8, 2011 at 2:29 Comment(2)
Thanks a lot! android:transcriptMode is magical :) you saved me!Dorelle
problem with this is that "if there was only a single element in my list view that it would be a the bottom of the screen."Follett
M
77

Try

listView.post(new Runnable(){
  public void run() {
    listView.setSelection(listView.getCount() - 1);
  }});

The 'post' seems to be required sometime in my experience, particularly if you have very recently updated the list.

Midshipman answered 29/3, 2010 at 10:10 Comment(4)
this works great. I tried android:stackFromBottom="true" before and the problem then is that if there was only a single element in my list view that it would be a the bottom of the screen. ThanksRentroll
Note: if the height of the last element is bigger than the height of the listview, the top of the last element will be aligned among the top of the list, i.e. the bottom of the element wont be visible. That's why the stackFromBottom solution is described by Ribose is better for chat-like functionality.Lucy
Worked like a charm! I also tried stackFromBottom="true" but since I was not clearing the list it didn't work for my situation. (At least I think that is why.)Knickknack
You're right, the post is necessary if this is called directly after notifyDataSetChanged(). Using post postpones the setSelection() until the update/refresh of the list is completed.Crete
M
2

I know its very late to answer but may be it will help someone. Using android:transcriptMode="alwaysScroll" will force the listview to scroll to bottom (as here we have used android:stackFromBottom="true") even if you'll try to scroll top which usually required most of the time. So instead of android:transcriptMode="alwaysScroll you can use android:transcriptMode="normal which will behave similar to the requirement of chat app and will not force the list of scroll always if the user want to see the content at the top.

Met answered 24/1, 2016 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.