Proper fix for FAB being hidden by keyboard (#2579)

This commit is contained in:
StellarSand
2025-07-25 23:34:24 +05:30
committed by GitHub
parent 8f6bd8c266
commit 996cd2cd2c
4 changed files with 31 additions and 14 deletions

View File

@@ -50,6 +50,7 @@ import androidx.palette.graphics.Palette;
import com.google.android.material.color.DynamicColors;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
@@ -1139,6 +1140,27 @@ public class Utils {
return WindowInsetsCompat.CONSUMED;
});
}
public static void applyWindowInsetsAndFabOffset(View root, FloatingActionButton fab) {
/* This function is a copy of applyWindowInsets, with the added behaviour that it ensures the FAB will be displayed vertically above the keyboard at all times */
ViewCompat.setOnApplyWindowInsetsListener(root, (view, windowInsets) -> {
Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
layoutParams.leftMargin = insets.left;
layoutParams.bottomMargin = insets.bottom;
layoutParams.rightMargin = insets.right;
layoutParams.topMargin = insets.top;
view.setLayoutParams(layoutParams);
// This is required to move the FAB above the keyboard when keyboard is open
Insets imeInsets = windowInsets.getInsets(WindowInsetsCompat.Type.ime());
boolean isKeyboardVisible = windowInsets.isVisible(WindowInsetsCompat.Type.ime());
fab.setTranslationY(isKeyboardVisible ? (- imeInsets.bottom) : 0);
return WindowInsetsCompat.CONSUMED;
});
}
public static ImageView.ScaleType getRecommendedScaleTypeForThumbnailImage(@Nullable Bitmap image) {
// Return something sensible if no image