DialogFragment not resizing when keyboard shown

柔情痞子 提交于 2019-11-28 07:31:30

Set the windowSoftInputMode property to adjustNothing in the AndroidManifest.xml of the activities that use the dialog fragment.

<activity
    ...
    android:windowSoftInputMode="adjustNothing">
...

and onCreateDialog to hide the soft input:

...
Dialog dialog = builder.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
return dialog;
}

FYI: https://developer.android.com/training/keyboard-input/visibility.html#ShowOnStart

I just use the following line in my DialogFragment:

getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

And nothing else, see here full example:

    public class TextEditor extends DialogFragment {

    public TextEditor () {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_text_editor, container);

        //set to adjust screen height automatically, when soft keyboard appears on screen 
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        //[add more custom code...]
        return view;
    }
}
Sara

Be sure that the layout is inside a scroll view:

<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  -->your layout here 
</ScrollView>

and follow Dirk comment:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_text_editor, container);

//add this line 
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

      //[add more custom code...]
      return view;
    }
Xavier

Even though it is a little late for a reply, since the question is in DialogFragment, the following code solves my issue.

@Override
public void onCreate(Bundle savedInstanceState) {
    ...

    // Setting STYLE_NO_FRAME allows popup dialog fragment to resize after keyboard is shown
    setStyle(DialogFragment.STYLE_NO_FRAME, R.style.theme_popupdialog_style);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.setCanceledOnTouchOutside(false);

    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    return dialog;
}

As for the style theme, I applied the following code

/** must put parent="@android:style/Theme.Dialog for it to work */
<style name="theme_popupdialog_style" parent="@android:style/Theme.Dialog">
    <item .... >...</item>
</style>

This can also be caused by:

<item name="android:windowTranslucentStatus">true</item>

Try to remove it from your theme.

Apart the changes mentioned in other answers also check the theme of dialogfragment
From my experiments, "android:windowIsFloating" attribute seems to affect how the window reacts to the soft input.

It you set this to false, the window won't slide up when the keyboard becomes visible.

This solution worked like a charm.

Inside styles:

    <style name="BottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/AppModalStyle</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowSoftInputMode">adjustResize</item>
    </style>

    <style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
        <item name="android:background">@drawable/rounded_corner_dialog</item>
    </style>

Here android:windowIsFloating should be false & android:windowSoftInputMode must be adjustResize.

Wrap layout inside NestedScrollView

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

     <--Rest of the layout-->
</androidx.core.widget.NestedScrollView>

As already mentioned, android:windowSoftInputMode="adjustResize" and dialog.getWindow().setSoftInputMode(WIndowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); are the right way of doing this.

BUT. If your view is not resizeable at all, then your buttons in the bottom will still be hidden. In my case, this hack was enough:

I set android:layout_weight for top views, so that when keyboard opens and dialog is resized — top views would be hidden:

Mahmoud

For DialogFragment, it seems that applying SoftInputMode only works if setted from inside the DialogFragment class, not the caller class:

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);    

}

Also, for onStart method, I added the following to expand dialog layout horizontally:

@Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!