Keyboard hides BottomSheetDialogFragment

后端 未结 6 1396
走了就别回头了
走了就别回头了 2020-12-04 13:22

There are more fields below the keyboard. This happened when i updated the support library. I know it\'s Kotlin but it looks almost the same as java. How do I fix this issue

相关标签:
6条回答
  • 2020-12-04 14:08

    This is working for me

    public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment 
    {
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle 
        savedInstanceState) {
    View v = inflater.inflate(R.layout.content_dialog_bottom_sheet, container, false);
    
    
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        return v;
     }
    
    0 讨论(0)
  • 2020-12-04 14:09

    This solution worked for me after spending 5 hours without luck:

    Step #1:

    Add this code to your styles.xml (located in res\values folder)

    <style name="CustomizedBottomDialogStyle">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:backgroundDimAmount">0.7</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowSoftInputMode">adjustResize</item>
        <item name="android:background">@android:color/transparent</item>
    </style>
    

    The key here is to set android:windowIsFloating -> false, if it is true your code will not work! Therefor i used rather android:backgroundDimEnabled and android:backgroundDimAmount to make background looks transparent with beautiful overlay.

    Step #2:

    Write this function to adjust it programmatically (note it is not optional, you need to perform both steps #1 and #2):

    private fun showDialog() {
    
        BottomSheetDialog(requireActivity(), R.style.CustomizedBottomDialogStyle).apply {
    
            window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
    
            setOnShowListener {
                Handler().post {
                    val bottomSheet = findViewById<View>(R.id.design_bottom_sheet) as? FrameLayout
                    bottomSheet?.let {
                        BottomSheetBehavior.from(it).state = STATE_EXPANDED
                    }
                }
            }
    
            setContentView(R.layout.dialog_layout)
    
            // Your code goes here....
    
            show()
        }
    }
    
    0 讨论(0)
  • 2020-12-04 14:10

    You can use the next class:

    import android.graphics.Rect; 
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.design.widget.BottomSheetBehavior;
    import android.support.design.widget.BottomSheetDialog;
    import android.support.design.widget.BottomSheetDialogFragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.WindowManager;
    
    public class TestBottomSheetDialog extends BottomSheetDialogFragment {
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View fragmentView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_bottom_sheet, container, false);
            if (getDialog().getWindow() != null) {
               getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            }
            if (getActivity() != null) {
                View decorView = getActivity().getWindow().getDecorView();
                decorView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
                    Rect displayFrame = new Rect();
                    decorView.getWindowVisibleDisplayFrame(displayFrame);
                    int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
                    int heightDifference = height - displayFrame.bottom;
                    if (heightDifference != 0) {
                        if (fragmentView.getPaddingBottom() != heightDifference) {
                            fragmentView.setPadding(0, 0, 0, heightDifference);
                        }
                    } else {
                        if (fragmentView.getPaddingBottom() != 0) {
                            fragmentView.setPadding(0, 0, 0, 0);
                        }
                    }
                });
            }
            getDialog().setOnShowListener(dialog -> {
                BottomSheetDialog d = (BottomSheetDialog) dialog;
                View bottomSheetInternal = d.findViewById(android.support.design.R.id.design_bottom_sheet);
                if (bottomSheetInternal == null) return;
                 BottomSheetBehavior.from(bottomSheetInternal).setState(BottomSheetBehavior.STATE_EXPANDED);
            });
            return fragmentView;
        }
    }
    
    0 讨论(0)
  • 2020-12-04 14:14

    I found the solution for 27 api. So the reason why keyboard hides view even with SOFT_INPUT_ADJUST_RESIZE is that the windowIsFloating is set for Dialogs.

    The most convenient way that I found to change this is by creating style:

    <style name="DialogStyle" parent="Theme.Design.Light.BottomSheetDialog">
        <item name="android:windowIsFloating">false</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowSoftInputMode">adjustResize</item>
    </style>
    

    And set this in onCreate method of your BottomSheetDialogFragment:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle)
    }
    

    This is how it looks on my device:

    0 讨论(0)
  • 2020-12-04 14:18

    add this to your styles

    <style name="DialogStyle">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="colorPrimaryDark">@android:color/transparent</item>
    </style>
    

    then in your's bottom sheet dialog's onCreate() add

    setStyle(DialogFragment.STYLE_NO_FRAME, R.style.DialogStyle);
    

    also don't forget to add to dialog's setupDialog() method

    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    
    0 讨论(0)
  • 2020-12-04 14:22

    I tried all of answers in this topic but nothing helped. I looked through many sites and found only one solution that working for me.

     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
    
        dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
        dialog.setOnShowListener {
            Handler().post {
                val bottomSheet = (dialog as? BottomSheetDialog)?.findViewById<View>(R.id.design_bottom_sheet) as? FrameLayout
                bottomSheet?.let {
                    BottomSheetBehavior.from(it).state = BottomSheetBehavior.STATE_EXPANDED
                }
            }
        }
    
        return dialog
    }
    

    Original solution

    0 讨论(0)
提交回复
热议问题