8

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?

FetFrumos
  • 5,388
  • 8
  • 60
  • 98

5 Answers5

7

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">
xleon
  • 6,201
  • 3
  • 36
  • 52
6

I got the answer in java from here : https://stackoverflow.com/a/28939113/4664754

Here is the C# version (tested and working):

public override bool DispatchTouchEvent(MotionEvent ev)
{
    if (ev.Action == MotionEventActions.Down)
    {
        View v = CurrentFocus;
        if (v.GetType() == typeof(EditText))
        {
            Rect outRect = new Rect();
            v.GetGlobalVisibleRect(outRect);
            if (!outRect.Contains((int)ev.RawX, (int)ev.RawY))
            {
                v.ClearFocus();
                InputMethodManager imm = (InputMethodManager)this.GetSystemService(Context.InputMethodService);
                imm.HideSoftInputFromWindow(v.WindowToken, 0);
            }
        }
    }
    return base.DispatchTouchEvent(ev);
}

This piece of code will hide the soft keyboard if you click on anything that is not an EditText. You just have to paste this in your activity class (for example your loginActivity)

yan yankelevich
  • 885
  • 11
  • 25
3

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);
borracciaBlu
  • 4,017
  • 3
  • 33
  • 41
2

Try my function:

public static void Close_AndroidKeyboard(Activity context){
    InputMethodManager inputManager = (InputMethodManager) context.getSystemService(
            Context.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(),
            InputMethodManager.HIDE_NOT_ALWAYS);
}

Sorry but i work with android studio, tell me if I helped you and good programming!

0

The easiest solution I've found, is to simply call the Unfocus() method on the control with focus (for instance an Entry or Editor). You can embed this call in an UnfocusOnClicked behaviour if you want to specify the function in the XAML directly.

Peter Waher
  • 178
  • 1
  • 8