Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView doesn't scroll to end, when keyboard using

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!

like image 262
Zakhar Fadeev Avatar asked Aug 23 '12 08:08

Zakhar Fadeev


2 Answers

Try this insead of your code:

listView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);

(or set it by XML layout).

like image 194
mac_cz Avatar answered Sep 20 '22 22:09

mac_cz


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);
like image 21
HitOdessit Avatar answered Sep 19 '22 22:09

HitOdessit