Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Hide keyboard when changing fragments

Tags:

android

When I change fragments. I am using this to close the keyboard because there is an EditText field on the screen. I just feel like there has to be a better way, but I havent found anything on detecting if the keyboard is on the screen.

Activity activity = getActivity();
InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
try
{
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
catch (Exception e)
{

}
like image 753
ios85 Avatar asked Feb 04 '13 23:02

ios85


People also ask

How do I make my keyboard invisible on Android?

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.

How do I prevent the soft keyboard from pushing my view up in fragment?

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.


1 Answers

In the activity where you implement the calling to various fragments, put the following...

    InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
like image 180
user2260040 Avatar answered Oct 24 '22 14:10

user2260040