36

I am trying to create an activity with a listview and a send message at bottom. The problem is that when the keyboard is shown, instead of pushing the bottom content, is just hiding it. I tried using adjustPan, but it pushes the hole view up (so there is no way to see the top elements of the listview and also de actionbar disappears).

If you take a look at WhatsApp or Line, the functionality is that when the last item of the list is shown at the bottom of the screen, the keyboard pushes up the listview (without taking the action bar or the first elements out of the screen), and when the last item of the list is not shown (after some scrolling up) the keyboard is hidden the bottom list (a normal adjustResize).

Anyone dealed with this issue?

thanks

EDIT:

I'll try to put a visual example:

So lets say this the ListView:

---item 1---  
---item 2---  
---item 3---  
---item 4---  
---EditText---

The editText is not part of the ListView, but a LinearLayout aligned at the bottom. When keyboard is shown, the ListView becomes like this (Item 3 and 4 are hidden by Keyboard):

---item 1---  
---item 2---  
---EditText---
---Keyboard---  

and what I would like to get is:

---item 3---  
---item 4---  
---EditText---
---Keyboard---  

I've tried the android:windowSoftInputMode="adjustPan".and the result is that effectivly item 3 and 4 are pushed up and not hidden by keyboard anymore. The problem is that it pushes the listview, but it pushes it out of the screen, so the actionBar disappears, and even if trying to scroll up, I can never see the Item 1 and 2 with the keyboard shown.

Hope I explained myself, not really easy..

Thanks

Mehdi
  • 447
  • 1
  • 5
  • 6

3 Answers3

54

Okay I have found a solution for you, what you want to do is essentially have the ListView scroll to the bottom every time. You can do this by:

ListView l = getListView();
l.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
l.setStackFromBottom(true);

If it is an Activity, do it in onCreate. If it is a Fragment, do it in onViewCreated.

wangyif2
  • 2,843
  • 2
  • 24
  • 29
  • 2
    Hi, thanks. It actually does scrolls the listView to the bottom. The problem is that when I scroll up the listview, and then show the keyboard, it scrolls again the listview to the bottom. This will make the user loose the item he was seeing. Just as an example have a look at your whatsapp/LINE app if you have it. when you open it, the listview is at the bottom, show the keyboard, you still see the last item,remove the keyboard and scroll up, then show the keyboard again, the listview is not scrolled to the bottom... Thanks again – Mehdi Apr 23 '13 at 18:24
  • Ahh, I see what you want, I have updated the answer, and tested it, should do exactly as you say. Please accept if it fixes it for you too. – wangyif2 Apr 23 '13 at 18:30
  • 2
    Damn it. I can't believe all lost time xD. Thanks – Mehdi Apr 23 '13 at 18:36
  • and do not forget to request focus as well. lstMessages.requestFocus(); then clear it and request other elements focus if needed – MSaudi Oct 29 '14 at 06:33
  • 2
    or in your layout add these lines to your `listView` : `android:transcriptMode="alwaysScroll" android:stackFromBottom="true"` –  Feb 09 '15 at 14:46
  • using listview.setStackFromBottom(true) makes the listview start from the bottom instead of from the top, so we can skip using listview.setStackFromBottom(true) ... just using listview.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL); can fulfill the requirement. – Narendra Singh Dec 30 '15 at 08:36
  • how about recyclerview ? – ralphgabb Feb 22 '17 at 09:10
13

In your layout XML:

android:stackFromBottom="true"
android:transcriptMode="normal"
Tim
  • 41,901
  • 18
  • 127
  • 145
s0i1
  • 144
  • 1
  • 10
1

Single line solution

list_view.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
Munish Kapoor
  • 3,141
  • 2
  • 26
  • 41