Close/hide the Android Soft Keyboard in MvxFragment
Asked Answered
S

5

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?

Samhita answered 18/6, 2015 at 15:52 Comment(0)
M
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);
Medusa answered 9/9, 2015 at 1:55 Comment(0)
W
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">
Woehick answered 19/6, 2015 at 12:58 Comment(0)
S
6

I got the answer in java from here : https://mcmap.net/q/81928/-edittext-clear-focus-on-touch-outside

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)

Saluki answered 17/5, 2016 at 12:23 Comment(0)
M
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);
Medusa answered 9/9, 2015 at 1:55 Comment(0)
O
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!

Olein answered 18/6, 2015 at 15:56 Comment(0)
C
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.

Caughey answered 8/1, 2022 at 20:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.