keep Scroll position with every refresh in list view

I set a timer in my app, I could get some information from a web service and resulted in a list view to display. Now my problem is that every time the timer runs, scroll back to the beginning …

how can i keep Scroll position with every refresh in list view ?

  • “FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS” affects getRootView().getHeight()
  • Android Text Selection In Webview
  • Android: Copy to clipboard selected text from a TextView
  • Android Studio and Gradle - build fails
  • Eclipse LogCat getting spammed by OpenGLRenderer
  • Fragments, with the same menu, on the same layout cause duplicated menuitem
  • part of my code:

    runOnUiThread(new Runnable() {
        public void run() {
             * Updating parsed JSON data into ListView
            * */
            ListAdapter adapter = new SimpleAdapter(DashboardActivity.this, 
                                                    new String[] { TAG_FULLNAME,
                                                                   TAG_MESSAGE }, 
                                                    new int[] {,
            // updating listview


    Related posts:

    Android - Acceleration down (smash)
    Implicit intent to uninstall application?
    Eclipse needs to be restarted frequently for logcat to work properly
    What does this Google Play APK publish error message mean?
    How to check if APK is signed or “debug build”?
    Android Cloud Printing with No Dialogs
  • Check progress for Upload & Download (Google Drive API for Android or Java)
  • how to add listener to autocompletetextview, android?
  • Adding Menu to ZXings barcodescanner, and squeezing the surfaceview
  • Combining ListActivity and ActionBarActivity
  • How to access data/data folder in Android device?
  • Listview items Animation Stop While Scrolling
  • 4 Solutions collect form web for “keep Scroll position with every refresh in list view”

    Do not call setAdapter(). Do something like this:

    ListAdapter adapter; // declare as class level variable
    runOnUiThread(new Runnable() {
        public void run() {
             * Updating parsed JSON data into ListView
            if (adapter == null) {
                adapter = new SimpleAdapter(
                        DashboardActivity.this, all_chat, R.layout.list_item, new String[]{TAG_FULLNAME, TAG_DATE, TAG_MESSAGE},
                        new int[]{,,});
            } else {
                //update only dataset   
                allChat = latestetParedJson;
                ((SimpleAdapter) adapter).notifyDataSetChanged();
            // updating listview

    You can add the following attribute to your ListView in xml.


    Add these attributes and your ListView will always be drawn at bottom like you want it to be in a chat.

    or if you want to keep it at the same place it was before, replace alwaysScroll to normal

    in the android:transcriptMode attribute. 


    There’s a good article by Chris Banes. For the first part, just use ListView#setSelectionFromTop(int) to keep the ListView at the same visible position. To keep the ListView from flickering, the solution is to simply block the ListView from laying out it’s children.

    I had the same issue, tried a lot of things to prevent the list from changing its scroll position, including:


    and not calling listView.setAdapter();
    None of it worked until I found this answer:

    Which looks like this:

    // save index and top position
    int index = mList.getFirstVisiblePosition();
    View v = mList.getChildAt(0);
    int top = (v == null) ? 0 : (v.getTop() - mList.getPaddingTop());
    // ...
    // restore index and position
    mList.setSelectionFromTop(index, top);


    ListView.getFirstVisiblePosition() returns the top visible list item. But this item may be partially scrolled out of view, and if you want to restore the exact scroll position of the list you need to get this offset. So ListView.getChildAt(0) returns the View for the top list item, and then View.getTop() - mList.getPaddingTop() returns its relative offset from the top of the ListView. Then, to restore the ListView‘s scroll position, we call ListView.setSelectionFromTop() with the index of the item we want and an offset to position its top edge from the top of the ListView.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.