Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 694
FetFrumos Avatar asked Jun 18 '15 15:06

FetFrumos


People also ask

How do you close hide the Android soft keyboard?

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

How do I close the keyboard on Android emulator?

Simply disable the Gboard app on your emulator. To do that, go to Settings > Apps > Gboard, and then click the "DISABLE" button. If Gboard is not visible, make sure you select the three dots in the top right corner and select the "Show system" option.

How do I unhide keyboard on Android?

It's in the "Keyboards & input methods" section of the menu. Tap Null Keyboard. Now, when you tap in a text field, no keyboard will appear. Tap a different keyboard under Current keyboard to re-enable the on-screen keyboard.


1 Answers

You can hide the soft keyboard giving focus to something that is not a "keyboard launcher" control, for example, the parent container of your auto-complete control.

parentContainer = FindViewById<LinearLayout>(Resource.Id.parentContainer);
parentContainer.RequestFocus();

Let´s say your parent container is a LinearLayout, you should allow it to get focus with these 2 properties:

<LinearLayout
    android:id="@+id/parentContainer"
    android:focusable="true"
    android:focusableInTouchMode="true">
like image 168
xleon Avatar answered Sep 27 '22 17:09

xleon