'How do I prevent the soft keyboard from pushing my view up in a fragment?
I have an edittext in my fragment. To prevent soft keyboard from pushing my view up I tried
android:windowSoftInputMode="adjustPan"
android:windowSoftInputMode="adjustNothing"
and programmatically i tried onResume and oncreateview methods of fragment
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
Above techniques didn't worked. But when I put scrollview as root view in fragment and put android:isScrollContainer="false" property then above methods are working... How can I make it work without scroll view
Solution 1:[1]
Use this in onCreateView() of fragment getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
Solution 2:[2]
Also, something to consider double-checking is if your fragment's are DialogFragments. Getting the window of my DialogFragment and then setting its soft input resolved my issue where the other solutions did not.
A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.
Source:https://developer.android.com/guide/topics/ui/dialogs.html
That being said, DialogFragment's have their own windows that can be set by doing the following in your onCreateDialog method.
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = // instantiate your dialog
//Get your dialog's window
Window window = dialog.getWindow();
if (window != null) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
}
return dialog;
}
Solution 3:[3]
Setting android:isScrollContainer = "false"
inside the ScrollView worked for me.
According to the documentation, settings "isScrollContainer" to true means that the scroll view can be resized to shrink its overall window so that there is space for an input method.
I suspect what is happening without setting this attribute to false is that the ScrollView is being treated as a scroll container by default and is being resized (shrunk) to allow the input method to appear. Everything outside of the ScrollView is shown because the ScrollView shrinks by as much as is necessary to show the input method and other views on the same hierarchy level.
Solution 4:[4]
If you only want to do this for specific fragments, do this:
- Save the current input mode
val inputMode = (activity as MainActivity).window.attributes.softInputMode
- Set the desired input mode in onCreateView()
(activity as MainActivity).window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
- Set the input mode back to what it was before in onDestroy()
override fun onDestroy() {
super.onDestroy()
inputMode?.let { (activity as MainActivity).window.setSoftInputMode(it)}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Haris Aftab |
Solution 2 | CJB |
Solution 3 | marshallb |
Solution 4 | nitnatsnoK |