getWindow().setSoftInputMode not working as expected

Viewed 19227

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?

4 Answers

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

You must do it like:

((AppCompatActivity)getContext()).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Related