Black Magic
Black Magic

Reputation: 2766

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

Upvotes: 3

Views: 1723

Answers (4)

Haider Malik
Haider Malik

Reputation: 1771

Looking at your code I am guessing that is an alert box of some sort. If you are using a custom dialog with a custom dialog class then add this line in the onCreate() of your custom dialog class, its been answered here: https://stackoverflow.com/a/53150825/7451779

Upvotes: 0

Muhammad Aamir Ali
Muhammad Aamir Ali

Reputation: 21097

use <activity android:windowSoftInputMode="adjustResize"> in your menifest. second add android:transcriptMode="alwaysScroll" property in your <ListView />

Upvotes: 3

Black Magic
Black Magic

Reputation: 2766

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! ;)

Upvotes: 0

Peter
Peter

Reputation: 1789

Are you using <activity android:windowSoftInputMode="adjustResize" ... > in your Manifest? http://developer.android.com/training/keyboard-input/visibility.html

Upvotes: 1

Related Questions