Set FAB (Floating Action Button) above keyboard

AndroidAndroid SoftkeyboardFloating Action-Button

Android Problem Overview


Already asked here but without a proper answer.

I want the FAB to float on top of the keyboard. that's it.

For example

  1. Open a new Blank Activity template project with Android Studio
  2. Change the Hello World TextView to EditText
  3. See image below:

enter image description here

Android Solutions


Solution 1 - Android

Turns out it's pretty easy,

  • Add android:windowSoftInputMode="adjustResize" to your activity in manifest
  • Make sure your root view in layout xml, has android:fitsSystemWindows="true" property

Solution 2 - Android

If you want to see FloatingActionBar move above keyboard, you should:

1) Insert FloatingActionBar inside CoordinatorLayout:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        android:src="@drawable/ic_img" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

2) Add in root View (where your FAB exist) code:

android:fitsSystemWindows="true".

For example:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context=".AddFilm">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="16dp"
            android:src="@drawable/ic_img" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</LinearLayout>

3) Add next code in AndroidManifest.xml to your Activity:

android:windowSoftInputMode="adjustResize"

For example:

<activity
        android:name=".MainActivity"
        android:windowSoftInputMode="adjustResize"/>

Solution 3 - Android

If you are using in particular fragment you can use this code and for activity just alter accordingly.

    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            view.getWindowVisibleDisplayFrame(r);
            if (v.getRootView().getHeight() - (r.bottom - r.top) > 500) {
                //Log.d("keyboardStatus","opened");
                fab.setVisibility(View.GONE);
            } else {
               // Log.d("keyboardStatus","closed");
                fab.setVisibility(View.VISIBLE);
            }
        }
    });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavidView Question on Stackoverflow
Solution 1 - AndroidDavidView Answer on Stackoverflow
Solution 2 - AndroidKirill ParfenovView Answer on Stackoverflow
Solution 3 - AndroidHarishView Answer on Stackoverflow