19

I have a requirement in which I have one MainActivity. From this activity I instantiate 4 fragments(Let us say FragmentA, FragmentB, FragmentC, FragmentD.

Out of these four Fragments; on 3 Fragments(Let us say FragmentA, FragmentB, FragmentC), I want to set MainActivity's windowsoftinputmode() to SOFT_INPUT_ADJUST_PAN so that window can resize.

And on one Fragment()let us say FragmentD), I want to set MainActivity's windowsoftinputmode() to SOFT_INPUT_ADJUST_NOTHING.

So what I do is, on each Fragment's onViewCreated() method I execute the following line of code with changing LayoutParams flag:

((MainActivity)getActivity()).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);

or

((MainActivity)getActivity()).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

Problem is that it not working and I am not able to setSoftInputMode correctly and I can not prevent window getting resized.

Same lines of code works perfectly when I execute them in onCreate() of MainActivity as shown :

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

But than the value is set for all four fragments. My MainActivity declaration in manifest is :

<activity
    android:name="com.test.MainActivity"
    android:launchMode="singleTask"
    android:screenOrientation="portrait"/>

Can somebody help what I am doing wrong in this. I want to have behaviour different for different Fragments.

Tarun Deep Attri
  • 8,174
  • 8
  • 41
  • 56

2 Answers2

40

try this I found :

aim to use two different modes for tabs. The modes are SOFT_INPUT_ADJUST_RESIZE and SOFT_INPUT_ADJUST_NOTHING.

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

The code line above simply doesn't work by itself. App always behaves in a state ADJUST_NOTHING. However, if windowSoftInputMode="adjustResize" is inserted into tag in AndroidManifest.xml file, app window is resized as default. Additionally, when you call following line in fragment

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);

it will behave as expected and nothing is resized. worked by me!

batsheva
  • 2,175
  • 1
  • 20
  • 32
  • 1
    did u try this? did it helped? – batsheva Oct 15 '17 at 08:39
  • yes, thing was that i was not specifying windowsSoftInputMode, and to change windowsSoftInputMode from Fragment, it needs to be declared in manifest first and than change programatically from Fragment. – Tarun Deep Attri Oct 16 '17 at 05:07
  • @Terri Can you share your code that worked, I'm currently going through this issue and nothing seems to work. – iBEK Dec 01 '17 at 06:17
  • @batsheva Hello there, thanks for that, it did help me a lot. However, now it's deprecated, are there any alternatives? 'SOFT_INPUT_ADJUST_RESIZE: Int' is deprecated. Deprecated in Java – Abdulrahman Hasanato Aug 01 '23 at 07:28
6

Adding implementation for iBEK's comment. Two things are required to be done for windowsSoftInputMode to work properly :

First is add following flag :

windowSoftInputMode="adjustResize"

in AndroidManifest.xml file as shown below :

    <activity
        android:name="com.test.TestActivity"
        android:windowSoftInputMode="adjustResize"/>

Specifying this flag is madatory for windowsSoftInputMode to work.

Secondly add following in onCreate of your Activity, TestActivity in this case :

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams. SOFT_INPUT_ADJUST_RESIZE);

Now it should work for all fragments that activity is hosting.. I hope this answers your question @iBEK.

Tarun Deep Attri
  • 8,174
  • 8
  • 41
  • 56
  • I actually did this and it just pushes the layout up to give the keyboard room at the bottom of screen. It may look fine on some larger devices but on the smaller devices, everything's on top of one another. – iBEK Dec 04 '17 at 17:58
  • Yes, that is the purpose of it right, resize app to accommodate Keyboard. You should adjust your view for such a scenario in which Keyboard is shown if you want any listener to perform any adjustments to view, you can use OnGlobalLayoutListener. Reference Link : https://stackoverflow.com/questions/4312319/how-to-capture-the-virtual-keyboard-show-hide-event-in-android Don't go for accepted answer but go for second answer which contains OnGlobalLayoutListener by @amalBit Use height difference to be "200 to 300" instead of 100 as given in answer. – Tarun Deep Attri Dec 05 '17 at 05:10