What I want is simple, well at least I thought it would be simple. I just want a window where an EditText is on the bottom of the screen, and the rest of the space is filled with ListView. Unfortunately, it did not work as I expected. What I want is the following image. Is there any easy way to do this in the XML, or should I write some special code for this?
My Problematic Android Source Code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/demolist"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
</ListView>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0">
</EditText>
</LinearLayout >
I created a layout engine for Android that allows you to listen for keyboard hidden and visible state changes (among many other core features). This library is available at http://phil-brown.github.io/AbLE/.
Simply add the
jar
to yourlibs
directory (create this folder if it does not exist), and instead of inheriting from anActivity
, inherit fromAbLEActivity
. Next, Override the methodsonKeyboardShown
andonKeyboardHidden
, and in these you can animate your content view up or down as needed:You need to set selection position... But - in a strange way, i don't really understand why - you need to do it with post()
So you can add a TextWatcher for your editText and you can call this method there... So your list will scroll to bottom.
You can use
android:windowSoftInputMode="adjustPan"
in your AndroidManifest.xml as belowThis should produce the output you want. Official android documentation .
Optionally you can specify transcript mode(if you are working on some chat feature) for your list view if you want to scroll automatically to the end of listview. You can check the
android:transcriptMode="normal"
as well and see if that fits.A related SO answer. Official android doc.
This should solve most of your issues, maybe except for the last one(in the image) which I haven't tested.
Just override the method of ListView#onSizeChanged(int w, int h, int oldw, int oldh) like below: PS:Your ListView's height can not be 0 anytime,if it may 0,not working.
I need this functionality too. Here's my attempt, but it doesn't quite work -- The scrolling is a bit erratic (doesn't always end up where it should) and it interferes with the preservation of the scroll position when rotating the screen.
What you Want
Last Visible Item Without Keyboard
Last Visible Item With Keyboard
Last Visible Item With larger input
Here is how i did it.
Summary
Details
Activity
Chat View