5

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.

manfcas
  • 1,933
  • 7
  • 28
  • 47
jph
  • 2,181
  • 3
  • 30
  • 55
  • why is that the soft keyboard is shown upon displaying the fragment?? can you post an image what it look like and a piece of code. – Rod_Algonquin Aug 11 '14 at 07:46
  • It was shown because the fragment contains EditTexts and the default soft input mode wasn't "stateHidden". So it automatically gave focus to the first EditText and brought up the soft keyboard. – jph Aug 13 '14 at 03:16

2 Answers2

19

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
   }
}
Manish Mulimani
  • 17,535
  • 2
  • 41
  • 60
  • @user3249477 Yes, I've tried it on my device before posting the answer. – Manish Mulimani Aug 11 '14 at 07:26
  • Weird. Somehow it doesn't for me. The keyboard just won't show. By the way the OP asked for state invisible not visible. – Simas Aug 11 '14 at 07:27
  • @user3249477 Hmm. Regarding visible flag you were right, I have updated the answer. Let's see whether it works for OP. – Manish Mulimani Aug 11 '14 at 07:29
  • 2
    For posterity: setting the soft input mode needs to happen in the fragment's onCreateView() method and not onCreate(). As follows: getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); – jph Aug 13 '14 at 03:15
-2

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

Ramkailash
  • 1,852
  • 1
  • 23
  • 19