Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 267
jph Avatar asked Aug 09 '14 04:08

jph


1 Answers

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
   }
}
like image 148
Manish Mulimani Avatar answered Nov 11 '22 03:11

Manish Mulimani