why position of BottomNavigationView depends on ScrollView scrollbars?

840 views Asked by At

Hi I'm little bit confused about BottomNavigationView position behavior.

Why is enabling/disabling of scrollbars affecting resizing of layout and position of BottomNavigationView?

I created simple example project to show my problem...

With this code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="sandbox.jarda.cz.bottomnavigationtest.MainActivity">

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="test" />

            </LinearLayout>
        </ScrollView>

    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation" />
</LinearLayout>

Will the BottomNavigationView stay under the Keyboard (when is opened)...

<code>BottomNavigationView</code> is under the keyboard

BUT :)

When I delete line/enable scroll bars

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

The main layout is "resized" and the BottomNavigationView is on the top of the keyboard...

<code>BottomNavigationView</code> is on the top of the keyboard

WHY?

Thx for answer:)

3

There are 3 answers

0
Pankaj Kumar On

The reason is ScrollView. When you open keyboard, it resizes itself with available space excluding keyboard + height of bottom navigation view.

When Keyboard not showing it uses available height, excluding height of navigation view.


For more clarity, implement keyboard listener and there calculate height of scroll view. You can modify below code for that

Calling code. Can be into onCreate

container.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener); // Here container is topmost LinearLayout in your layout

And mOnGlobalLayoutListener would be

private ViewTreeObserver.OnGlobalLayoutListener mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        Rect r = new Rect();
        mActivityRootView.getWindowVisibleDisplayFrame(r);
        int screenHeight = mActivityRootView.getRootView().getHeight();
        int keyboardheight = screenHeight - r.bottom;
        /**
         *
         * We are taking 15% of the screen height as it should be sufficient space for the keyboard to determine if keyboard is open.
         */
        if (keyboardheight > screenHeight * 0.15) {
            // Keyboard showing
            // PRINT HEIGHT OF SCROLLVIEW while keyboard is showing
        } else {
            // Keyboard hidden
            // PRINT HEIGHT OF SCROLLVIEW while keyboard not showing 
        }
    }
};
4
AskNilesh On

it is not because of scrollbars just you need to just specify windowSoftInputMode use android:windowSoftInputMode="adjustPan|adjustResize" in your activity in manifest tag like this

<activity android:name=".MainActivity"
        android:windowSoftInputMode="adjustPan|adjustResize">
    <intent-filter>
       <action android:name="android.intent.action.MAIN" />

       <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
0
Shaifali Rajput On

This is not because of scrolling, this is because of keyboard behaviour of your application.

you need to specify windowSoftInputMode in your activity tag of your manifest for more about windowSoftInputMode check this link

<application ... >
<activity
    android:windowSoftInputMode="adjustResize" ... >
    ...
</activity>
...