Home » Android » android – ListView doesn't scroll to end, when keyboard using

android – ListView doesn't scroll to end, when keyboard using

Posted by: admin June 15, 2020 Leave a comment

Questions:

My ListView should automatically scroll to end, when new message received or sent.
But when I use Android keyboard to send message from EditText below ListView, ListView resized and new message, which I want to send, appears out of screen and I have to scroll down to see it, even if keyboard disappears and ListView resizes again.

To scroll ListView I use:

listView.postDelayed(new Runnable() {
@Override public void run() { listView.setSelection(listView.getCount()); } }, 100);

but is not working correctly in my case.

Does anyone know why it may occur? Is there a way, which ALWAYS scroll ListView to end?

Thanks, your help is much appreciated!

How to&Answers:

You can try to add this line of code after you set selection to your list:

listView.smoothScrollToPosition(listView.getCount());

Resulting code should look like this:

listView.postDelayed(new Runnable() {
        @Override
        public void run() {
            listView.setSelection(listView.getCount());
            listView.smoothScrollToPosition(listView.getCount());
        }
    }, 100);

Answer:

Try this insead of your code:

listView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);

(or set it by XML layout).

Answer:

Set android:windowSoftInputMode in manifest for the activity to adjustPan|adjustResize.