The4thIceman
The4thIceman

Reputation: 3889

Android - A blank space remains when the keyboard disappears

I am having an issue with the keyboard. When it disappears, the space it occupied remains blank and the rest of the layout does not adjust

normal screen:

enter image description here

with keyboard:

enter image description here

keyboard dismissed:

enter image description here

I have never seen this before, so I am not even sure where to start looking. This happens on 4.2.2 as well as 5.1

The other piece of important information is that this is a custom LinearLayout that holds all of the content. Maybe it has something to do with that. I can upload any code if necessary.

This is the main layout file shell.

<com.edgetechlabs.app_v2.MasterLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Menu (Drawer)-->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:visibility="visible"
    android:layout_height="match_parent"
    android:orientation="vertical" >
     <ScrollView
        android:id="@+id/activity_main_menu_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/drawer_background"
        android:cacheColorHint="#00000000" >
     </ScrollView>
</LinearLayout>
<!-- Fragment Holder -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!-- This is where fragment will show up -->
    <FrameLayout
        android:id="@+id/fragment_master"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>
</LinearLayout>

This is my manifest file

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MasterActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Upvotes: 9

Views: 5645

Answers (4)

CoolMind
CoolMind

Reputation: 28803

In my case I have android:windowSoftInputMode="adjustResize" and it doesn't matter. In one layout when a list is too short and a keyboard shows, then hides, the list occupies smaller space. A blank space appears below the list (where the keyboard was shown).

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        ... >

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <include
                layout="@layout/details"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap" />

        </com.google.android.material.appbar.AppBarLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="com.example.behavior.AutoSizeBehavior"> // Wrong class

            <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </FrameLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

I tried to change app:layout_scrollFlags="scroll|exitUntilCollapsed|snap", but it was not good for scrolling. Then I replaced CoordinatorLayout with NestedScrollView, it helped, but the layout changed. After that I noticed that app:layout_behavior referred to a custom class (with bugs). I changed it to @string/appbar_scrolling_view_behavior. You can also change to com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.

Upvotes: 0

Kebab Krabby
Kebab Krabby

Reputation: 1699

if you dont have set adjustPan set in your activity in AndroidManifest.xml f.e you need adjustResize then this was the only solution which helped me with that white blank space

EditText.setOnFocusChangeListener { v, hasFocus ->
    if (hasFocus) {
        // something if needed
    } else {
        Utils.hideKeyboardToAdjustPan(activity)
    }
}

fun hideKeyboardToAdjustPan(activity: Activity?) {
    activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
    hideKeyboard(activity)
}

fun hideKeyboard(activity: Activity?) {
    if (activity != null) {
        hideKeyboard(activity.currentFocus)
    }
}

fun hideKeyboard(view: View?) {
    if (view != null) {
        val inputManager = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputManager.hideSoftInputFromWindow(
            view.windowToken,
            WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED
        )
    }
}

Upvotes: 0

Momen Zaqout
Momen Zaqout

Reputation: 1498

try to change ScrollView to androidx.core.widget.NestedScrollView

this helped me to fix the problem

Upvotes: 0

The4thIceman
The4thIceman

Reputation: 3889

I added

android:windowSoftInputMode="adjustPan"

to my manifest within the activity tag, and now it works. I have never had to do that before, I guess my custom layout was messing with the keyboard somehow.

Thanks to @eee comment which pointed me in the right direction

Upvotes: 15

Related Questions