I have a fragment that has some EditTexts that need to be filled by the user. There's a problem that the soft keyboard hides the EditText so that the user cannot see what they're writing. I don't want the user to scroll manually, I want the fragment to adjust so that it fits the keyboard automatically.
IMPORTANT NOTE: I have seen lots of suggestions to add android:windowSoftInputMode="adjustResize"
or android:windowSoftInputMode="adjustPan"in manifest. However, I want this action to be only for a specific fragment and not for all fragments within the activity.
I have tried adding requireActivity().window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
In onCreateView or in onCreate but nothing seems to change.
In my case,
android:windowSoftInputMode="adjustPan" and ScrollView
it's work for Fragment in Activity.
Related
I have an EditText visible in a LinearLayout that I don't want to move when the soft keyboard is shown. The reason I don't want it to move is because I want to control the items above and adjust their sizes to make the EditText and the content it controls below to become visible.
Is it possible to prevent the screen moving at all once the soft keyboard is shown?
Note: I am using an activity with the toolbar hidden.
I have solved this - I have added the SetSoftInputMode to AdjustNothing to the OnCreate method. Doing this in the xml doesn't do anything, but in the code does.
In your AndroidManifest.xml file, go to your activity and add this.
<activity
...
android:windowSoftInputMode="adjustNothing"
/>
your views will remain in the same position
After looking up various questions on stack overflow, I have found that many other people had the following problems:
Background gets resized when soft keyboard opens
Soft keyboard opens when activity starts
The solution to both of these lies in the ActivityManifest.xml.
To prevent the background image from being resized when the soft keyboard opens, you can add android:windowSoftInputMode="stateVisible|adjustPan" to the <activity> in the manifest.
To prevent the soft keyboard opening when the activity starts, you can add android:windowSoftInputMode="stateHidden" to the <activity> in the manifest.
The fact that one solution requires stateHidden and the other requires stateVisible means that I cannot use both solutions. I am looking to prevent the soft keyboard from stealing focus on activity start but also prevent the soft keyboard from resizing the background when the user does decide to focus on the EditText.
Is there a viable solution to both of these issues?
The fact that one solution requires stateHidden and the other requires
stateVisible means that I cannot use both solutions.
Yes. But, you can use stateHidden|adjustPan.
Keyboard will not pop up unless user clicks on an EditText. And potential changes to your background will be in terms of positioning; scaling of the background will not occur.
If your EditText is wrapped within a parent container, set android:focusableInTouchMode="true" for that container. Basically, make that container receive the initial focus when the activity starts. Take a look at this link
Is any part of your layout a scrollable container (like ListView)? If so, try setting android:isScrollContainer="false" on that item in your XML layout.
If you have any views which have android:isScrollContainer="true", the layout manager will attempt to resize your layout when the keyboard pops up, regardless of `android:windowSoftInputMode'.
Here's a similar question: What does android:isScrollContainer do?
There are basically two ways to fulfill your requirement.
Just use the below activity tag
android:windowSoftInputMode="adjustPan|stateAlwaysHidden"
Or
Using
android:windowSoftInputMode="stateVisible|adjustPan" to the activity tag in the manifest.
Now with your EditText use following android:focusableInTouchMode="true" with android:focusable="false".
android:windowSoftInputMode is set normally set on an Activity but I set everything up as one Activity that switches to different Fragments to support tabbing and I need different soft input mode for fragments.
My actual problem is that adjustPan causes text views within webviews to get covered by the keyboard and adjustResize was resizing a view that I was using for calculations and I thought setting different soft input mode for each fragment would be a good workaround.
try this each onCreateView of your fragment
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
i hope its works for you and reply
I have an activity with four tabs and every tab is implemented as a fragment. In one of these tabs I would like to prevent the soft keyboard from pushing up the view, while the keyboard should still push the views up in the other fragments. Does anybody know how to achieve this?
I cannot use the activity's windowSoftInputMode flag, because that would prevent it for the whole activity with all four fragments.
Try to change flag dynamically
Use the following to change the softInputMode for an Activity in your tab click listener.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Same problem that I've read on other posts. I don't want the soft keyboard to pop up when the Activity loads. The above code snippet works, however my View no longer scrolls up so that the User can still see the EditText.
It scrolls without this line of code. Any ideas?
Set right attribute on your activity in manifest
http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft