ListView scroll to selected item

Android

Android Problem Overview


I have a ListView with an edit text and a button below it. When I click on a listView item the keyboard appears and push up the edit text and the button. I want the list to scroll to the selected item. Any idea? Thanks

Android Solutions


Solution 1 - Android

You can use ListView's setSelection(int position) method to scroll to a row.

Solution 2 - Android

You could use ListView's smoothScrollToPosition(int position) to scroll to a particular location in the list.

Solution 3 - Android

For a direct scroll:

getListView().setSelection(11);

For a smooth scroll:

getListView().smoothScrollToPosition(11);

To Scroll to top

getListView().setSelectionAfterHeaderView();

Note

>try to call it in post because sometime listview is not yet created while you calling it's method

getListView().postDelayed(new Runnable() {          
    @Override
    public void run() {
        lst.setSelection(15);
    }
},100L);

Solution 4 - Android

You should use transcript mode:

getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);

Solution 5 - Android

Setup a listener on your the list item being clicked, then use View.getTop() or View.getBottom() when clicked to get it's position within the parent. You can then use ListView.scrollTo(x, y) to scroll to the list item.

Solution 6 - Android

Yo can look For

> listView.setSelectionFromTop(position, distanceFromHeader);

It will position the Item at position , specified pixels below the top of listview

Solution 7 - Android

You can use

smoothScrollToPosition(position)

Just increase the position of item with 1, and you will get the view of item.

getListView().smoothScrollToPosition(position + 1);

Solution 8 - Android

Using duration gives a better user experience. Use this, with duration added. Will scroll the item in position smoothly to the top of the listview.

int duration = 500;  //miliseconds
int offset = 0;      //fromListTop

listview.smoothScrollToPositionFromTop(position,offset,duration);
  • descrease duration to make scrolling faster

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnitaView Question on Stackoverflow
Solution 1 - AndroidandroidworkzView Answer on Stackoverflow
Solution 2 - AndroidRajeshView Answer on Stackoverflow
Solution 3 - AndroidZar E AhmerView Answer on Stackoverflow
Solution 4 - AndroidJin35View Answer on Stackoverflow
Solution 5 - AndroidDogeView Answer on Stackoverflow
Solution 6 - AndroidPavan JajuView Answer on Stackoverflow
Solution 7 - AndroidPrashant Kumar SharmaView Answer on Stackoverflow
Solution 8 - AndroidAdamView Answer on Stackoverflow