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

@@ -75,7 +75,8 @@
<activity
android:name=".ManageGroupActivity"
android:label="@string/group_edit"
android:theme="@style/AppTheme.NoActionBar"/>
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustResize"/>
<activity
android:name=".LoyaltyCardViewActivity"
android:exported="true"

View File

@@ -24,7 +24,6 @@ import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
@@ -45,6 +44,9 @@ import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.exifinterface.media.ExifInterface;
import androidx.lifecycle.ViewModelProvider;
@@ -298,7 +300,7 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity implements
super.onCreate(savedInstanceState);
binding = LoyaltyCardEditActivityBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
Utils.applyWindowInsets(binding.getRoot());
Utils.applyWindowInsetsAndFabOffset(binding.getRoot(), binding.fabSave);
viewModel = new ViewModelProvider(this).get(LoyaltyCardEditActivityViewModel.class);
@@ -681,15 +683,7 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity implements
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
View view = getCurrentFocus();
if (view != null && imm.isAcceptingText()) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
view.clearFocus();
}
else {
askBeforeQuitIfChanged();
}
askBeforeQuitIfChanged();
}
});
}
@@ -1523,7 +1517,7 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity implements
int id = item.getItemId();
if (id == android.R.id.home) {
getOnBackPressedDispatcher().onBackPressed();
askBeforeQuitIfChanged();
return true;
}

View File

@@ -48,7 +48,7 @@ public class ManageGroupActivity extends CatimaAppCompatActivity implements Mana
super.onCreate(inputSavedInstanceState);
binding = ActivityManageGroupBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
Utils.applyWindowInsets(binding.getRoot());
Utils.applyWindowInsetsAndFabOffset(binding.getRoot(), binding.fabSave);
Toolbar toolbar = binding.toolbar;
setSupportActionBar(toolbar);

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