Question:
How to use the latest WindowInset API to adjust space betweeen my dialog and softkeyboard?
I have a BottomSheetDialog with some EditText. The default animation will show the soft keyboard right below my EditText which will cover my save button. After doing some research, I added this line into my BottomSheetDialog
fragment
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
And it worked (as the picture is shown down below)!
But apparently SOFT_INPUT_ADJUST_RESIZE
is deprecated.
* @deprecated Call {@link Window#setDecorFitsSystemWindows(boolean)} with {@code false} and
* install an {@link OnApplyWindowInsetsListener} on your root content view that fits insets
* of type {@link Type#ime()}.
And I couldn't figure out how to use the new OnApplyWindowInsetsListener
to achieve the same effect.
Here is my current BottomSheetDialog
fragment:
public class BottomSheetDialog extends BottomSheetDialogFragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// Adding this line works, but it's deprecated in API 30
// getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
getDialog().getWindow().setDecorFitsSystemWindows(false);
view = inflater.inflate(R.layout.fragment_bottom_dialog_cash, container, false);
view.setOnApplyWindowInsetsListener((v, insets) -> {
Log.d("dialog", "onCreateView: ");
Insets imeInsets = insets.getInsets(WindowInsets.Type.ime());
v.setPadding(0,0,0,imeInsets.bottom);
return insets;
});
return view;
}
I use an onclicklistener in another fragment to show this dialog. Code in Another fragment
@Override
public void onItemClick(int position) {
BottomSheetDialog dialog = new BottomSheetDialog();
dialog.show(getParentFragmentManager(), "BottomSheetDialog");
}
In fact, the log indicates that the listener is never triggered when the soft keyboard pop up. FYI, I'm following this video and this blog.