jph jph - 2 months ago 7
Android Question

How to make fragment honor soft input mode of activity

I have an activity configured as follows in the manifest:


This activity creates a fragment. The fragment configures itself to be full-screen in onCreate(), e.g.:


The fragment's layout is roughly this:

<!-- a fixed height header -->
<!-- some EditTexts -->
<!-- a fixed height footer -->

Unfortunately, when the fragment is displayed the soft keyboard is automatically shown and input mode is "adjustResize" instead of "adjustPan". This causes the footer to always be visible; when the keyboard is shown the ScrollView just shrinks in height.

How can I configure the fragment to have the "stateHidden|adjustPan" behavior? I'm getting the fragment functionality from the support library, if that matters.


Activity and on-screen soft keyboard both have got their own window. android:windowSoftInputMode indicates how main window of activity should interact with window of on-screen soft keyboard.

I believe you are using DialogFragment. It has it's own window which floats on top of the activity's window. Hence the flags set for Activity through android:windowSoftInputMode are not applicable to DialogFragment.

One possible solution is set those flags for DialogFragment's window programmatically. Use getDialog to retrieve the underlying dialog instance, retrieve the window for the dialog using getWindow and specify the flags for soft input mode using setSoftInputMode.

public static class MyDialog extends DialogFragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Dialog layout inflater code
        // getDialog() need to be called only after onCreateDialog(), which is invoked between onCreate() and onCreateView(). 
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
       // return view code