8

In my Android project I want the softInputMode for just one fragment to be adjustPan.

Adding the following line to my manifest (inside the activity) works as expected:

android:windowSoftInputMode="adjustPan"

But following lines in my fragment do nothing:

@Override
public void onResume() {
   super.onResume();
   getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}

@Override
public void onPause() {
    super.onPause();
    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);
}

Any ideas why that is and what could be done to fix?

Matt Ke
  • 3,599
  • 12
  • 30
  • 49
Budius
  • 39,391
  • 16
  • 102
  • 144

4 Answers4

3

You are setting the soft input mode for the activity, i'm not sure if it will work but try:

myFragment.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

EDIT:

I suppose you are working with the onPause and onResume of the fragment, have yout tried using the ones of the parent activity? The result might be the same thou, because some times they are connected.

Ayoub
  • 341
  • 1
  • 13
  • 5
    Fragments don't have `getWindow()` (https://developer.android.com/reference/android/app/Fragment.html) and they're inside the activity. – Budius Dec 10 '13 at 10:34
  • Yes, those are the pause/resume from the fragment. As specified on the question, I want the `adjustPan` behavior only for that one fragment which does not pause/resume with the activity. – Budius Dec 10 '13 at 10:55
  • Are you sure the fragment goes through the onPause() and onResume() methods? If you just replace the first fragment it will not go through onPause(), and when resuming (with backpress) the first one will not fire it's onResume(). – Ayoub Dec 10 '13 at 11:12
  • I'm sure they go through. The previous frag doesn't go through pause/resume if the transaction is "add()". And there're several other method calls on pause/resume that were omitted for brevity that work as expected. Furthermore, running in debug mode I can break-point the lines without issue. – Budius Dec 10 '13 at 11:33
  • Try setting the input method as shown by jegumi in his answer to this: [link](http://stackoverflow.com/questions/8751660/softkeyboard-does-not-display-for-a-newly-displayed-fragment) – Ayoub Dec 10 '13 at 11:43
  • that's not what the `adjustPan` option is for. AdjustPan pans the layout upwards to give space for the keyboard. I already can control when the keyboard shows/hides with no problem. Thanks anyway. – Budius Dec 10 '13 at 12:01
2

Try to also set the android:windowSoftInputMode="adjustPan" in your Manifest. I know it looks redundant to set the soft input mode in the manifest and in your code but that's the only way it worked for me.

Rob
  • 136
  • 1
  • 8
  • After many attempts I returned to setting this attribute in Manifest. A programmaticall call in a fragment doesn't work. – CoolMind Aug 30 '16 at 19:42
0

Implement in 'onCreate' method of your activity.

 @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }
coder
  • 312
  • 3
  • 12
0

You must do it like:

((AppCompatActivity)getContext()).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77