Search code examples
androidlistviewandroid-listviewkeyboard

Android keyboard overlaps ListView


I have a problem, I have a ListView and an EditText designed like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ShoutboxTab" >

    <ListView
        android:id="@+id/shoutbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/shoutbox_field"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="14dp"
        android:layout_marginRight="20dp" >
    </ListView>

    <Button
        android:id="@+id/shoutbox_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/shoutbox_field"
        android:text="Shout" />

    <EditText
        android:id="@+id/shoutbox_field"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/shoutbox_button"
        android:layout_alignLeft="@+id/shoutbox"
        android:ems="10" >

        <requestFocus />
    </EditText>

</RelativeLayout>

So the EditText is under the ListView. When I have items added to the ListView and I click the EditText to type, the keyboard overlaps the bottom part of the ListView. I want to make sure the bottom part of the ListView goes up with the keyboard. Is this possible? Thanks


Solution

  • I fixed it like this:

    V.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    int heightDiff = V.getRootView().getHeight() - V.getHeight();
                    if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                        shoutbox.setSelection(adapter.getCount() - 1);
                    }
                 }
            });
    

    Where V is the view.

    Propably not the nicest way, but it is the easiest! ;)