Push Listview when keyboard appears without adjustPan
Asked Answered
C

3

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

Cornwall answered 21/4, 2013 at 16:54 Comment(2)
can you post a screen shot of what it looks like currently, its not very clear to me what the problem is.Waldrop
I've added an example to try to explain. ThanksCornwall
W
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.

Waldrop answered 23/4, 2013 at 17:23 Comment(7)
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 againCornwall
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.Waldrop
Damn it. I can't believe all lost time xD. ThanksCornwall
and do not forget to request focus as well. lstMessages.requestFocus(); then clear it and request other elements focus if neededHonour
or in your layout add these lines to your listView : android:transcriptMode="alwaysScroll" android:stackFromBottom="true"Animalist
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.Cyanine
how about recyclerview ?Haire
C
13

In your layout XML:

android:stackFromBottom="true"
android:transcriptMode="normal"
Curch answered 8/1, 2014 at 14:28 Comment(1)
android:stackFromBottom="true" not neededCyanine
D
1

Single line solution

list_view.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
Dogmatics answered 11/8, 2015 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.