How to hide the soft keyboard inside a fragment?
Solution 1
As long as your Fragment creates a View, you can use the IBinder (window token) from that view after it has been attached. For example, you can override onActivityCreated in your Fragment:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
}
Solution 2
Nothing but the following line of code worked for me:
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Solution 3
If you add the following attribute to your activity's manifest definition, it will completely suppress the keyboard from popping when your activity opens. Hopefully this helps:
(Add to your Activity's manifest definition):
android:windowSoftInputMode="stateHidden"
Solution 4
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my, container,
false);
someClass.onCreate(rootView);
return rootView;
}
Keep an instance of my root view in my class
View view;
public void onCreate(View rootView) {
view = rootView;
Use the view to hide the keyboard
public void removePhoneKeypad() {
InputMethodManager inputManager = (InputMethodManager) view
.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
IBinder binder = view.getWindowToken();
inputManager.hideSoftInputFromWindow(binder,
InputMethodManager.HIDE_NOT_ALWAYS);
}
Solution 5
Exception for DialogFragment
though, focus of the embedded Dialog
must be hidden, instead only the first EditText
within the embedded Dialog
this.getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
WilHall
I'm a software engineer. My favorite language is Python.
Updated on October 12, 2021Comments
-
WilHall over 2 years
I have a
FragmentActivity
using aViewPager
to serve several fragments. Each is aListFragment
with the following layout:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp"> <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <EditText android:id="@+id/entertext" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
When starting the activity, the soft keyboard shows. To remedy this, I did the following inside the fragment:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //Save the container view so we can access the window token viewContainer = container; //get the input method manager service imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); . . . } @Override public void onStart() { super.onStart(); //Hide the soft keyboard imm.hideSoftInputFromWindow(viewContainer.getWindowToken(), 0); }
I save the incoming
ViewGroup container
parameter fromonCreateView
as a way to access the window token for the main activity. This runs without error, but the keyboard doesn't get hidden from the call tohideSoftInputFromWindow
inonStart
.Originally, I tried using the inflated layout instead of
container
, i.e:imm.hideSoftInputFromWindow(myInflatedLayout.getWindowToken(), 0);
but this threw a
NullPointerException
, presumably because the fragment itself isn't an activity and doesn't have a unique window token?Is there a way to hide the soft keyboard from within a fragment, or should I create a method in the
FragmentActivity
and call it from within the fragment? -
WilHall over 12 yearsThanks, this is what I ended up doing. However, I would still like to know how to use the Input Method Manager to show/hide the keyboard, because I might need to use it sometime after the activity starts.
-
andro-girl almost 12 yearsi added this to my project.but when i click on another tab it is crashing.
-
Matjaz Kristl almost 10 yearsThis is the only way to hide keybord, if you have DialogFragment.
-
hBrent over 9 years
SOFT_INPUT_STATE_HIDDEN
also worked for me, though I don't know what the difference between that and `SOFT_INPUT_STATE_ALWAYS_HIDDEN' is. -
MrEngineer13 over 8 yearsI used this but I used getView() from my fragment instead of keeping an instance of my view.
-
Mobile Applications about 8 yearsThe onCreate is a class outside the Fragment, so I pass it the rootView to be able to use it to remove the phoneKeyPad in this class. I suppose they wanted it from inside the Fragment not a class in the Fragment.
-
moujib almost 8 yearsThe first answer didn't work , this one did the trick . Thanks
-
Aurasphere over 7 yearsFor those who are doing this and getting a crash with a NullPointerException, just use the InputMethodManager inside your fragment onCreateView method. Doing so, you will have your view and you can change the last line using the view you have inflated to imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
-
Mstack over 7 yearswhere to write this?
-
Harish Reddy over 5 yearsThanks for saving my time buddy.
-
Ronny Sulistio over 5 yearsif you have tab fragment and want to hide keyboard for few tab only, use this.
-
Sami Issa over 4 years@Mstack, works on onActivityCreated() method.
override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)}
-
Dark almost 4 yearsit doesn't work on api < 26, but this does (inside fragment class) @Override public void onResume() { super.onResume(); getView().setFocusable(true); getView().setFocusableInTouchMode(true); getView().requestFocus(); }
-
Programming Padawan almost 4 yearsMy situation is I am using fragment/TabView. The first tab has "Tips" in a TextView. The second tab has an activity where I have EditText(s) with "editText1.setShowSoftInputOnFocus(false);" command set and my own custom keypad. When I put the app to the background and then bring the app back into view the unwanted soft keyboard would pop-up. Setting the above command in the onStart Life Cycle Override method stops this. Thanks @Shajeel Afzal
-
Duc Trung Mai over 3 yearsWorks but you have to check if
getActivity().getCurrentFocus().getWindowToken()
is not null otherwise it'll cause error if there's no focused editText. See my answer below -
Sherif farid about 3 yearsperfect answer , and I suggest to add same function to on Resume to handle it in case of many fragments created inside viewpager and one of them need to hide keypad after created
-
truthsayer over 2 yearsYou just saved me more than 48 hours of work bro!