Search code examples
androidandroid-softkeyboardandroid-bottomnav

How to avoid only BottomNavbar to pushed up when soft keyboard appears


I have a view like this in my android app

enter image description here

There is a mainActivity which has that bottomNavBar and there is fragment inside activity which has two buttons at bottom.

What I am supposed to do is that when soft keyboard appears bottomNavBar should stay at bottom (behind the keyboard) but those two buttons should be pushed up and visible on top of keyboard

If I set android:windowSoftInputMode="adjustResize" for mainActivity in manifest file then keyboard push up bottomNavBar and buttons aswell.

If I set android:windowSoftInputMode="adjustResize" only to fragment (programmatically) then it still shows same behavior.

How can I do that? Any suggestions will be highly appreciated

Here is my Activity xml

    <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <LinearLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/mobile_navigation"
            android:layout_weight="@integer/int_two"/>

        <com.google.android.material.bottomnavigation.BottomNavigationView
            app:itemIconTint="@drawable/bottom_navigation_selector"
            app:itemTextColor="@drawable/bottom_navigation_selector"
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="@integer/int_zero"
            android:background="?android:attr/windowBackground"
            app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
            app:menu="@menu/bottom_nav_menu" />

    </LinearLayout>
</layout>

Solution

  • I also face the same problem, after putting multiple efforts I find a solution which work for me.What I do, when keyboard appers make bottomNavBar visibility GONE and vice-versa.

    Try this for getting keyboard open and closed event -

    view.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
          Rect r = new Rect();
          view.getWindowVisibleDisplayFrame(r);
          if (view.getRootView().getHeight() - (r.bottom - r.top) > 500) {
            // on Keyboard Open
            llBottomNavBarLayout.setVisibility(View.GONE);
          } else {
            // on keyboard close
            llBottomNavBarLayout.setVisibility(View.VISIBLE);
          }
        });
    

    For getting view I use

    LayoutInflater layoutInflater = getLayoutInflater();
    View view = layoutInflater.inflate(R.layout.activity_abc, null);