androidxamarinmvvmcrossandroid-softkeyboard

Close/hide the Android Soft Keyboard in MvxFragment


I create android app with xamarin + mvvmcross. I have an MvxAutoCompleteTextView in my MvxFragment. After writing in the MvxAutoCompleteTextView and clicking on the others controls, I want to hide the virtual keyboard. I use this code

public class MyFragment : MvxFragment 
{
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState)
    {

        base.OnCreateView(inflater, container, savedInstanceState);
        var view = this.BindingInflate(Resource.Layout.frMy, null);
        var autoComplete = view.FindViewById<MvxAutoCompleteTextView>(Resource.Id.acMy);
        InputMethodManager inputManager = (InputMethodManager)inflater.Context.GetSystemService(Context.InputMethodService);
        inputManager.HideSoftInputFromWindow(autoComplete.WindowToken, HideSoftInputFlags.None);
        return view;
    }
}

but this not work. How do I hide the keyboard?


Solution

  • Try this:

    InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
    imm.HideSoftInputFromWindow(autoComplete.WindowToken, 0);
    

    the value 0 in HideSoftInputFromWindow is the const Android.Views.InputMethods.HideSoftInputFlags.None so you can use the equivalent syntax:

    InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
    imm.HideSoftInputFromWindow(autoComplete.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);