jph
jph

Reputation: 2233

How to make fragment honor soft input mode of activity

I have an activity configured as follows in the manifest:

android:windowSoftInputMode="stateHidden|adjustPan"

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

setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme);

The fragment's layout is roughly this:

<LinearLayout>
  <!-- a fixed height header -->
  <ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1.0">
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
      <!-- some EditTexts -->
    </LinearLayout>
  </ScrollView>
  <!-- a fixed height footer -->
</LinearLayout>

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.

Upvotes: 5

Views: 10061

Answers (2)

Ramkailash
Ramkailash

Reputation: 1850

You can close and open keyword manually in fragment. Detect the Window size run time and check the visiable view height is it match or not.

Because manifest do not know about fragment. Fragment is like as a collection of component view. We can use inside activity anywhere

Upvotes: -2

Manish Mulimani
Manish Mulimani

Reputation: 17615

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
   }
}

Upvotes: 19

Related Questions