Programmatically scroll to a specific position in an Android ListView Programmatically scroll to a specific position in an Android ListView android android

Programmatically scroll to a specific position in an Android ListView


For a direct scroll:

getListView().setSelection(21);

For a smooth scroll:

getListView().smoothScrollToPosition(21);


For a SmoothScroll with Scroll duration:

getListView().smoothScrollToPositionFromTop(position,offset,duration);

Parameters
position -> Position to scroll to
offset ---->Desired distance in pixels of position from the top of the view when scrolling is finished
duration-> Number of milliseconds to use for the scroll

Note: From API 11.

HandlerExploit's answer was what I was looking for, but My listview is quite lengthy and also with alphabet scroller. Then I found that the same function can take other parameters as well :)


Edit:(From AFDs suggestion)

To position the current selection:

int h1 = mListView.getHeight();int h2 = listViewRow.getHeight();mListView.smoothScrollToPositionFromTop(position, h1/2 - h2/2, duration);  


Put your code in handler as follows,

public void timerDelayRunForScroll(long time) {        Handler handler = new Handler();         handler.postDelayed(new Runnable() {                       public void run() {                   try {                    lstView.smoothScrollToPosition(YOUR_POSITION);                } catch (Exception e) {}            }        }, time);     }

and then call this method like,

timerDelayRunForScroll(100);

CHEERS!!!