Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set windowsoftinputmode programmatically from Fragments?

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.

like image 625
Tarun Deep Attri Avatar asked Oct 15 '17 05:10

Tarun Deep Attri


People also ask

How do you move the layout up when the soft keyboard is shown Android fragment?

Set fitsSystemWindows = "false" and set android:windowSoftInputMode="adjustResize" in manifest file. Show activity on this post. I have used like this to show the soft keyboard programatically and this is worked for me to prevent the auto resize of the screen while launching the keyboard.


2 Answers

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!

like image 147
batsheva Avatar answered Oct 17 '22 04:10

batsheva


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.

like image 43
Tarun Deep Attri Avatar answered Oct 17 '22 05:10

Tarun Deep Attri